Distributed Mind

Quick and Dirty Batch Image Resize in Python

Want to resize many images all at once? This is a quick way to do so in Python using the Python Imaging Library.

Assuming you have all files by themselves in a directory, either enter the following in IDLE (the Python GUI) or save it in a file (with the ".py" extension) and then run it:

import os
import Image

prefix = "[some text to add to beginning of filenames to distinguish them from the old ones]"
os.chdir("[path to images]")
files = os.listdir(".")
for file in files:
	im = Image.open(file);
	w = im.size[0]
	h = im.size[1]
	x = desired width/w
	new_w = int(w*x)
	new_h = int(h*x)
	im = im.resize((new_w, new_h))
	im.save(prefix+file, "JPEG")

Alternatively, set x to some proportion, such as 0.5.

Lest anyone overestimate my abilities, I got most of this out of the PIL tutorial and the Python Library Reference.

(The quality of the resize images leaves a little bit to be desired, but I did say this was the quick and dirty way...)

posted at 20:48:15 on 07/09/05 by ben - Category: Technology

Comments

No comments yet

Add Comments

Want to comment? You'll need to create an account first.