Batch-Resizing Images in Python with Pillow | by Joe T. Santhanavanich | Towards Data Science

Sometimes you need to edit a ton of images for your project. For example, in web development, the bulk image resizing is done to make all images smaller and lighter to optimize transfer speed through the web. In Data Science, you might need to increase DPI of all your images for publishing in a scientific article. Managing all of them yourself in the image manipulation software might be boring. So, you may try to use the Pillow to do this job automatically for you!
This short article will show you a step-by-step of how to use the Pillow in Python focusing on batch-resizing images with an example script. Even though this Pillow library was introduced for a long time already, it is still powerful and very useful as in 2020.
For example, the above gif image shows that Python with Pillow can resize/rescale all image files in a folder with only ~8-10 lines of code. Several operations can be done with Pillow such as resize, rescale, rotate, transform, convert between file formats, enhance contrast, and much more.
You can simply install the Pillow library using pip as follows:
$ pip install pillow
The latest Pillow is no longer support with Python 2.x. You may check the Pillow version to match with your Python version below.
The concept of Pillow is pretty simple with these three steps
Load an image (Or ton of images)Do some image editing/ manipulation operationsSave the edited image in a format you like.
Load an image (Or ton of images)
Do some image editing/ manipulation operations
Save the edited image in a format you like.
from PIL import Image# ---- 1 ----im = Image.open("input_path")# ---- 2 ----# ... Do some operation# ---- 3 ----im = Image.save("output_path")
The Pillow supports a wide variety of raster file formats. Over 30 different file formats can be identified and read by the library. Write support is less extensive, but most common interchange and presentation formats are supported. [1]
Now, let’s see some real examples:
You can loop through each image file in folder using pathlib or os then use Pillow to load the image. After that, you can use thumbnail command to smaller the image size down to the specified max-width or max-height value and preserve the aspect of the image.
# Load image im = Image.open(input_img_path)# Smaller image/ preserve the aspect of the imageresized_im = im.thumbnail(<maxwidth>, <maxheight>)# Or Resize image to exact specified sizeresized_im = im.resize(<width>, <height>) # Then, save the resized imageresized_im.save(<output_img_path>, <file type>, dpi, ...)
For example, the script below shows how to resize all images in your folder called “input” to have a maximum width and height of 512 pixels and preserve the aspect of the image and save them with the same name as JPEG format in 300 dpi in “output” folder. This example uses pathlib to loop through all files in a folder.
With this script, you may add any image operations between lines 7 and 8. Such as rotating, transforming, converting between file formats, enhance contrast. You may see a list of the available operations from the Pillow documentation.
Also, there are several more Python Image Manipulation tools to play with such as OpenCV and scikit Image.
So, that’s about it. I hope you like this article and found it useful for your daily work or projects. Feel free to leave me a message if you have any questions or comments.
About me & Check out all my blog contents: Link
Be Safe and Healthy! 💪
Thank you for Reading. 📚
[1] Alex Clark https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html (2020), Pillow (PIL Fork)