How To Easily Resize & Compress Your Images In Python

James Phoenix
James Phoenix

As different social media platforms often require different image and width formats, resizing images automatically with python can definitely save you time.


Learning Outcomes

  • To learn how to resize a single image.
  • To learn how to resize multiple images within the current working directory.

from PIL import Image
import PIL
import os
import glob

How To Resize A Single Image With Python

As well as compressing an image, we can also re-size the image to be either:

  • A specific base width.
  • A specific base height.
base_width = 360
image = Image.open('example-image.jpg')
width_percent = (base_width / float(image.size[0]))
hsize = int((float(image.size[1]) * float(width_percent)))
image = image.resize((base_width, hsize), PIL.Image.ANTIALIAS)
image.save('resized_compressed_image.jpg')

Now let’s understand the code above line by line:

  1. We set a base width which we would like the image to be.
  2. Then we open the image with Image.open(‘image_name.jpg’)
  3. We calculate the aspect ratio for width by doing: base_width / the images existing width.
  4. This provides us with a ratio number that we can times by the height to get the correct height for producing our base_width.
  5. Then we resize the image based on the two values (base_width and hsize).
  6. The image is then saved with image.save(‘image_name.jpg’).

We can also do the exact opposite and get a specified height with the following code:

base_height = 360
image = Image.open(‘fullsized_image.jpg')
hpercent = (base_height / float(image.size[1]))
wsize = int((float(image.size[0]) * float(hpercent)))
image = image.resize((wsize, base_height), PIL.Image.ANTIALIAS)
image.save(‘resized_image.jpg')

How To Create A Thumbnail Whilst Preserving The Aspect Ratio

It is also possible for us to create a thumbnail image of an image using:

img.thumbnail(size, resample=3, reducing_gap=2.0)

Make this image into a thumbnail.  This method modifies the
image to contain a thumbnail version of itself, no larger than
the given size.  This method calculates an appropriate thumbnail
size to preserve the aspect of the image
picture = Image.open('example-image.jpg')
picture.thumbnail(size=(200,200))
print(picture.size)
# (200, 133)

How To Resize Multiple Images In The Current Working Directory

directory_files = os.listdir()
multiple_images = [file for file in directory_files if 'example' in file and file.endswith(('.jpg', '.png'))]

Firstly we list all of the files and folders inside of the current working directory. Then we find any .jpg and .png files that have the word example in their name.

Leanpub Book

Read The Meta-Engineer

A practical book on building autonomous AI systems with Claude Code, context engineering, verification loops, and production harnesses.

Continuously updated
Claude Code + agentic systems
View Book
print(multiple_images)
# ['example-image.jpg', 'example-image-two.jpg']

# Looping over all of the images:

for image in multiple_images:

    img = Image.open(image)
    img.thumbnail(size=(300,300))
    print(img)

    # We would run the command below to save the images:
    # img.save('resized-image_'+image, optimize=True)

Then we loop over every image, open it and then resize the image.


How To Resize & Compress Multiple Images In The Current Workng Directory

However in our scenario, we would like not only to reduce the size of the images but also to compress them, therefore we will add on the following paramter to this line:

img.save('resized-image_'+image, optimize=True, quality=30)
for image in multiple_images:
    img = Image.open(image)
    img.thumbnail(size=(300,300))
    print(img)
    img.save('resized-image_'+image, optimize=True, quality=30)

Now you can easily both resize and compress your images using Python 🥰!

In the next tutorial you’ll learn how to convert jpg and png images to next generation image formats such as .webp 🔥

Newsletter

Become a better AI engineer

Weekly deep dives on production AI systems, context engineering, and the patterns that compound. No fluff, no tutorials. Just what works.

Join 306K+ developers. No spam. Unsubscribe anytime.


More Insights

Cover Image for Hand Off Between Agent Subscriptions Before You Run Out of Weekly Usage

Hand Off Between Agent Subscriptions Before You Run Out of Weekly Usage

The session about to hit its weekly limit is the single best-informed writer of the brief that lets a different vendor’s agent pick up the work. Spend the last of the allowance on that brief, not on one more edit.

James Phoenix
James Phoenix
Cover Image for Use an Expensive Model to Build a Cheap Agentic Runtime

Use an Expensive Model to Build a Cheap Agentic Runtime

The model that writes the harness and the model that runs inside it do not have to be the same model. They probably should not be.

James Phoenix
James Phoenix