How to Resize an Image in Python?


In this blog post, we’ll be showing you how to resize an image using the Python programming language. Don’t worry if you’ve never written a line of Python code before; we’ll walk you through each step of the process. With just a few lines of code, you’ll be able to take an image of any size and make it smaller or larger to fit your needs. Let’s get started!

Image Resizer

Step One: Import the Python Image Library (PIL) Module

The first thing you’ll need to do is import the PIL module. PIL is a free library that adds image processing capabilities to Python. You can install it using either pip or easy_install by running either of the following commands in your terminal window:

pip install pillow or easy_install pillow

Once PIL is installed, you’re ready to move on to the next step.

Step Two: Open the Image You Want to Resize

Next, open up the image you want to resize in your favorite text editor. We’ll be using the “deer.jpg” image from Unsplash for this example. Once you have your image open, save it as a new file called “resized_image.py”. This will help keep things organized and will make it easier to run our script when we’re finished writing it.

Step Three: Resize Your Image

Now, we’ll add some code to our newly created “resized_image.py” file that will actually resize our image for us. Copy and paste the following code into your file:

from PIL import Image original_image = Image.open("deer.jpg") #Open the original image #Set the new width and height values width = 500 height = 500 resized_image = original_image.resize((width, height)) #Resize the image resized_image .save("resized_deer.jpg") #Save the resized image
Code language: Python (python)

This script will take our original image and resize it so that its width and height are both 500 pixels. You can change these dimensions to whatever you like; just be sure that both values are equal so that your resulting image doesn’t end up being stretched or squished.

Finally, this script will save our new, resized image as a file called “resized_deer .jpg”. Again, feel free to name this file whatever you like; just make sure that you also update line 9 of our script accordingly.

Now that our script is written, we can go ahead and run it by entering the following command into our terminal window:

python resized_image .py
Code language: CSS (css)

If everything went according to plan, you should now have a new, resized copy of your original image saved in the same directory as your script with whatever name you chose for it on line 9.

And that’s all there is to it! In just three easy steps, you can take any image and resize it using Python.

Another Python script to Resize an Image

A full source code

import sys import os.path from argparse import ArgumentParser from PIL import Image parser = ArgumentParser() parser.add_argument('--file', dest='File', help='Input image filename', required=True) parser.add_argument('--width', dest='Width', help='Width in pixels', type=int) parser.add_argument('--height', dest='Height', help='Height in pixels', type=int) args = parser.parse_args() if not args.Width and not args.Height: print('Error: Either width or height must be specified') sys.exit(1) def get_output_dimensions(input_image, args): width, height = input_image.size if not args.Width: ratio = args.Height / height output_width = round(width * ratio) else: output_width = args.Width if not args.Height: ratio = args.Width / width output_height = round(height * ratio) else: output_height = args.Height return (output_width, output_height) try: input_image = Image.open(args.File) width, height = get_output_dimensions(input_image, args) resized_image = input_image.resize((width, height)) file_extension = os.path.splitext(args.File)[1] resized_image.save('output%s' % (file_extension)) except Exception as err: print('Could not resize image: %s' % (err)) sys.exit(1)
Code language: Python (python)

Changes the size of an image based on its width and height.

You can give the following arguments:

  • file The path or name of the image file. width In pixels, the width.
  • If not given, the height is used to resize the image while keeping the aspect ratio.
    height In pixels, height.
  • If nothing is given, the image is resized based on the width given, while the aspect ratio stays the same.

How to run python script

$python image_resizer.py --width 200 --height 400 --file input_image.jpg
Code language: PHP (php)

Output
The resized image file named output with the same extension of the input file is generated within the same folder of the image_resizer.py script.

Conclusion:

As you can see, using Python to resize an image is incredibly simple and only takes a few lines of code. Whether you need to reduce an image’s size for upload purposes or make it larger so that people can see it more clearly, Python makes it easy. So go ahead and give it a try yourself! You might be surprised at how useful this little trick can be .

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts