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!
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:
Code language: Python (python)
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
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:
Code language: CSS (css)
python resized_image .py
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
Code language: Python (python)
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) resized_image.save('output%s' % (file_extension)) except Exception as err: print('Could not resize image: %s' % (err)) sys.exit(1)
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
Code language: PHP (php)
$python image_resizer.py --width 200 --height 400 --file input_image.jpg
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.
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 .