Skip to content
Muslu Yüksektepe

pil_otomatik_resim_boyutlandirma

A script that shrinks the images in a folder with PIL and places them on a common background so they all end up the same size.

pil_otomatik_resim_boyutlandirma is a short Python script that shrinks the images in a folder without distorting them and places each one in the middle of a white background of the same size. In the end, every image, whatever its original size and ratio, becomes a PNG file of the same dimensions. I wrote it in January 2017 with Python's PIL library.

Purpose

When images such as logos or product pictures need to be shown in a grid on a web page, the page looks messy if each image has a different width and height. Forcing the images straight to the same size, on the other hand, makes them look squashed or stretched. I wrote this script to fit images into the same frame without distorting them, and to do it for a whole folder in one go instead of one by one by hand.

How it works

The script first lists the files in the given folder and keeps only those with the wanted extensions, which are jpg, jpeg and png by default. It then applies three steps to each image.

In the first step it opens the image with PIL and shrinks it with the thumbnail method so that it is at most 360 x 360 pixels. This method keeps the image's aspect ratio and does not enlarge images that are already smaller than that size. The shrunk image is saved as a PNG in the thumb folder.

In the second step it creates an empty 360 x 360 pixel image with a white background and an alpha (transparency) channel.

In the third step it takes the final width and height of the shrunk image and calculates how much space to leave on the left and at the top so that it sits in the center of the background. It pastes the image at that position and saves the result as a PNG in the son folder.

Key features

  • Because aspect ratios are kept, no image looks squashed or stretched.
  • Small images are not enlarged, so they do not get blurry; they are only centered.
  • All outputs are the same size, which gives a tidy grid on the page.
  • The target size, background size and extensions to process can be changed easily through the variables at the top of the script.

Technical stack

The script is written in Python 2 and uses the PIL library for image work. The file list is read with the os module. The README explains what each line does with Turkish comments. Alongside the script, the repository holds sample input images and their processed versions in the thumb and son folders.

Status

The script has not been updated since 2017. Because it uses Python 2 syntax and the ANTIALIAS constant from PIL at the time, running it today with Pillow and Python 3 needs a few small fixes. The path of the folder to process is also hard-coded in the script.

Use cases

  • It can be used to bring the logos and pictures in a site's references or products section to the same size.
  • It helps turn product photos from different sources into uniform square images on e-commerce sites.
  • It offers a commented example for anyone who wants to learn how to shrink images, create new images and place one image on top of another with PIL.
Category
Python libraries
Year
2017

Technologies

  • Python
  • PIL
  • Image Processing

Links

  • GitHub https://github.com/muslu/pil_otomatik_resim_boyutlandirma

Back to all projects