Recently, I had the need to resize several images. If I only had a handful of images to resize, I could have easily opened and resized them one at a time. However, in this case, I had more than 400 images that needed to be resized. You can see why I chose to write a tool to do that for me. Since I had the need for some code to resize images, I’m sure there are plenty of others that could benefit from the same code. So, I’m going to take a minute to share that code with you. I will first show you how to resize individual images. Then, I will take it to the next step and resize multiple images just like my recent use case.
For resizing images, Python provides us with everything we need right out-of-the-box. To begin with, you will need to include an import for the “Image” library.
Next, you will need to open the image using the “open” function and save it to a variable.
img_name = “some_image.jpg”
img1 = Image.open(img_name)
Once you have your image open, you can call the “resize” function which accepts 2 parameters. The first argument the resize function accepts is a tuple containing width and height respectively. The second parameter the resize function accepts tells Python which algorithm to use when resizing your image. You can choose from “NEAREST”, “BILINEAR”, “BICUBIC”, and “ANTIALIAS”.
img2 = img1.resize((width, height), Image.NEAREST)
Now that you have resized your image, the only thing you have left to do is save it. To save your resized image, simply call the “save” function passing it the name of the image you want to save your resized image as. In this case, I will save my resized image using the same name as the original, overwriting the original.
Here is the code to resize a single image. You’ll notice that I created variables for storing the image name, width, and height. This allows me to reuse the same information at different times without needing to rewrite the same info. But, you can shrink this code tremendously by placing the image name, width, and height in-line instead of as separate variables.
import Image img_name = "some_image.jpg" img1 = Image.open(img_name) width = 320 height = 240 img2 = img1.resize((width, height), Image.NEAREST) #img2 = img1.resize((width, height), Image.BILINEAR) #img2 = img1.resize((width, height), Image.BICUBIC) #img2 = img1.resize((width, height), Image.ANTIALIAS) img2.save(img_name)
Pretty simple, huh? Well, as promised, I will now show you how to resize multiple images. For that, we will need to add an import for the “os” library. We will use functions from this library to iterate over the files within a directory and resize them if they are images. The folder that contained the images I needed to resize also included other files such as videos and documents. To keep from my app blowing up when trying to resize files that were not images, I went with the approach of getting the file extension and checking that the extension is of type “JPG”. There are other ways of doing this, but the method I am showing here worked just fine for me.
Instead of going over the code that I’ve already gone over above, I will just show you the entire code I used. You will notice that the code expects 2 folders. The first folder contains all of the original images and the second folder is where I saved my resized images.
import os import Image def file_type(f): return f.split(".")[-1] indir = 'images/' outdir = 'output/' listing = os.listdir(indir) for f in listing: if file_type(f) == 'JPG': img = Image.open(indir + f) img.resize((320, 240), Image.NEAREST).save(outdir + f)
PayPal will open in a new tab.