Batch-resize images in Python: a small Pillow script that runs on an 8GB mini PC
A small Pillow script that resizes a folder of images in one go. It keeps the aspect ratio, leaves the originals in place, and processes one file at a time, so it does not strain an 8GB / no-GPU PC.
Blog headers, email attachments, screenshots for a document. Resizing an image takes a minute each, but ten or twenty of them add up. In this article we build a small Python script that resizes every image in a folder.
How this works: The script never overwrites the originals. It writes resized copies to a separate folder and processes one image at a time, so an 8GB PC never has to hold dozens of large images in memory at once. Processing time depends on image count and size. Try about ten images first, then check the output size and quality.
Tested on Windows 11 / Core i5-7260U / PC RAM 8GB / no external GPU. Behavior can differ with the Pillow version and the image format.
What you need
- Python 3.12 (any working Python installation is fine)
- Pillow, the image library
- A folder of images to resize
Install Pillow with one command in Command Prompt:
python -m pip install Pillow
If it is already installed, you will see "Requirement already satisfied". On a work PC with software restrictions, check the IT policy first.
The script
Save the following as resize_images.py in Notepad or VS Code.
from pathlib import Path
from PIL import Image, ImageOps
# Input and output folders (change these for your machine)
SRC = Path(r"C:\work\images")
DST = Path(r"C:\work\images_resized")
MAX_SIZE = 1600 # Maximum long edge in pixels
QUALITY = 85 # JPEG quality (1-95)
DST.mkdir(parents=True, exist_ok=True)
for path in sorted(SRC.iterdir()):
if not path.is_file():
continue
if path.suffix.lower() not in {".jpg", ".jpeg", ".png", ".webp"}:
continue
try:
with Image.open(path) as im:
im = ImageOps.exif_transpose(im)
im.thumbnail((MAX_SIZE, MAX_SIZE))
out = DST / (path.stem + ".jpg")
if im.mode in ("RGBA", "LA") or (im.mode == "P" and "transparency" in im.info):
background = Image.new("RGB", im.size, "white")
background.paste(im, mask=im.convert("RGBA").split()[-1])
im = background
else:
im = im.convert("RGB")
im.save(out, "JPEG", quality=QUALITY, optimize=True)
print("OK", path.name, "->", out.name)
except Exception as e:
print("NG", path.name, ":", e)
Four details matter:
thumbnail()keeps the aspect ratio and shrinks the image to fit inside the given box. It never enlarges.ImageOps.exif_transpose()applies the rotation stored by phone cameras.- Transparent PNGs get a white background before conversion. Without it, transparent areas can turn black in JPEG.
- Files are closed after each iteration, so the script does not load the whole folder into memory.
How to run it
- Set
SRCto the folder with the originals andDSTto the output folder. - Open Command Prompt in the folder that contains the script.
- Run
python resize_images.py. - Open the output folder and check the aspect ratio and quality.
The output keeps the original name and changes only the extension to .jpg. Because the originals stay in place, you can adjust MAX_SIZE or QUALITY and run it again.
Choosing a size
| Use | Long edge | Note |
|---|---|---|
| Blog header | 1200-1600px | If your layout fixes the width, resize by width instead |
| Email attachment | 1000-1280px | Mail servers also limit total message size |
| Screenshot in a document | Around 1600px | Zoom in and check that the text is still readable |
Start with QUALITY = 85. If the file is still too large, try 80 or 75. Text-heavy screenshots get hard to read if you go too low.
Variation: fix the width only
To set the width to 800px and let the height follow the aspect ratio, replace thumbnail() with:
width = 800
height = round(im.height * width / im.width)
im = im.resize((width, height), Image.LANCZOS)
Applying the same width to a portrait image can produce an unexpected size. If you need different rules for portrait and landscape, branch on im.width >= im.height.
Common problems
- HEIC files fail: Pillow cannot always open iPhone HEIC files. Add
pillow-heifor convert them to JPEG first. - Transparent PNGs turn black: the script adds a white background. Change
"white"if you need another color. - Names collide: if
photo.jpgandphoto.pngare in the same folder, both becomephoto.jpg. Include the original extension in the name or split them into subfolders. - Small images stay small:
thumbnail()never enlarges. Useresize()if you really need to, but quality will suffer. - It takes a while: thousands of large images will keep the CPU busy. Try ten files first, then run the whole folder.
Notes
- Keep a backup and never point
DSTat the source folder. Otherwise you risk overwriting the originals. - Customer photos and internal screenshots can contain personal or confidential information. Check where the output is stored and who can access it.
- Check copyright and license terms before editing or redistributing images that belong to someone else.
- On a work PC, confirm that running scripts and installing libraries is allowed.
Summary
Batch resizing needs only Pillow's thumbnail() and pathlib. Keep the aspect ratio, leave the originals alone, and process one file at a time. Those three rules make it safe on an 8GB / no-GPU machine. Start with a folder of ten images, check the output size and quality, then tune MAX_SIZE for your use.