Resize an image in Python

Shrink an image without squashing it, blurring it or making it bigger by accident.

To resize an image in Python you use Pillow, the standard image library (pip install pillow). The call itself is one line. The part that needs care is the arithmetic that keeps the picture in proportion.

The short answer

from PIL import Image

img = Image.open("photo.jpg")

width = 600
height = round(img.height * width / img.width)

resized = img.resize((width, height), Image.Resampling.LANCZOS)
resized.save("photo-600.jpg")

print(resized.size)  # (600, 400)

That scales a 1200x800 photo down to 600 wide and works out the matching height. If Pillow itself is new to you, start with Pillow in Python.

PIL resize image: what resize actually does

Image.resize takes one argument that matters: a (width, height) tuple. It returns a new image and leaves the original alone. It also does exactly what it is told, with no regard for proportions:

squashed = img.resize((300, 300))
print(squashed.size)  # (300, 300)

A 3:2 photo forced into a square comes out visibly squashed. Two other slips are common: passing the numbers separately as img.resize(300, 300), which fails because the size must be a single tuple, and calling img.resize(...) on its own line and wondering why img has not changed. Keep the return value.

How to resize an image in Python and keep the aspect ratio

The short answer does the sum by hand, which is the right choice when you want an exact width. When you want "fit inside this box", let thumbnail do it:

thumb = img.copy()
thumb.thumbnail((400, 400))
print(thumb.size)  # (400, 267)

thumbnail differs from resize in three ways:

  • It keeps the aspect ratio. The size you pass is a maximum, not a target.
  • It changes the image in place and returns None, so img = img.thumbnail(...) leaves you with nothing. Copy first if you still need the original.
  • It never makes an image bigger.

If you would rather get a new image back, ImageOps.contain does the same fitting and returns the result:

from PIL import ImageOps

fitted = ImageOps.contain(img, (400, 400))
print(fitted.size)  # (400, 267)

Python Pillow resize image quality: resampling filters

When Pillow shrinks an image it has to decide how to blend many pixels into one. The filter controls that:

  • NEAREST is the fastest and looks jagged. It is right for pixel art and nothing else.
  • BILINEAR and BICUBIC are the middle ground. BICUBIC is the default for both resize and thumbnail.
  • LANCZOS is the sharpest for downscaling photos, and a little slower.
sharp = img.resize((600, 400), Image.Resampling.LANCZOS)

Older code uses Image.ANTIALIAS for the same filter. That name was removed in Pillow 10 and now raises AttributeError: module 'PIL.Image' has no attribute 'ANTIALIAS'. Replace it with Image.Resampling.LANCZOS.

Do not upscale by accident

resize and ImageOps.contain will happily enlarge a small image, and the result is soft and blurry:

big = ImageOps.contain(img, (2400, 2400))
print(big.size)  # (2400, 1600)

thumbnail will not, which makes it the safe choice for user uploads. If you need resize, check the size first:

def shrink_to_width(img, max_width):
    if img.width <= max_width:
        return img.copy()
    height = round(img.height * max_width / img.width)
    return img.resize((max_width, height), Image.Resampling.LANCZOS)


print(shrink_to_width(img, 800).size)   # (800, 533)
print(shrink_to_width(img, 3000).size)  # (1200, 800)

Python image library resize for a folder of images

Loop over the files with pathlib and write the results to a separate folder, so a bug cannot overwrite your originals:

from pathlib import Path

out = Path("resized")
out.mkdir(exist_ok=True)

for path in sorted(Path("photos").glob("*.jpg")):
    with Image.open(path) as photo:
        photo.thumbnail((800, 800))
        photo.save(out / path.name, quality=85)

The with block closes each file as soon as it is saved, which matters once the folder has a few thousand images in it.

The shorter way with Nitro Image

Nitro Image wraps Pillow and builds the defaults above into the call: give it a width and it keeps the aspect ratio, uses Lanczos, and refuses to upscale unless you pass allow_upscale=True. Its class is also called Image, so import it under another name next to Pillow:

from nitro_img import Image as NitroImage, BatchImage

NitroImage("photo.jpg").resize(800).save("photo-800.jpg")

BatchImage("photos/*.jpg").resize(800).webp().save("web/{name}.webp")

The second line is the folder loop, with a conversion to WebP thrown in. That is where it pays for itself: batches, several output sizes, format changes. For one image in one script, the four lines of Pillow at the top of this page are plenty.

Common questions

Does resizing change the original file? No. Nothing touches the disk until you call save, and then only the path you give it. Save under a new name and the original is untouched.

How do I resize by percentage? Multiply both sides: img.resize((img.width // 2, img.height // 2)) halves the image.

Why is my resized JPEG larger than I expected? File size depends on the save settings as well as the dimensions. Pass quality=85 or lower when saving.

How do I get an exact size without squashing? Resizing alone cannot change the shape of a picture. You need to crop as well, which ImageOps.fit does in one step. See crop an image.

This page uses Nitro Image.

Resize, convert, and optimise images with a chainable API. Generate responsive sizes in one call.

$ pip install nitro-image