regular expression concatenation with strings

Jacek Trzmiel sc0rp at
Fri Jun 22 22:41:36 CEST 2007


oscartheduck wrote:
> I have a little script that sits in a directory of images and, when
> ran, creates thumbnails of the images. It works fine if I call the
> function inside the program with something like "thumbnailer("jpg),
> but I want to use a regular expression instead of a plain string so
> that I can match jpeg, jpg, JPEG etc.

Something like this will work:

#!/usr/bin/env python
#from PIL import Image
import glob, os, re

size = 128, 128

def thumbnailer(dir, filenameRx):
     for picture in [ p for p in os.listdir(dir) if 
os.path.isfile(os.path.join(dir,p)) and filenameRx.match(p) ]:
         file, ext = os.path.splitext(picture)
         im = (picture)
         im.thumbnail(size, Image.ANTIALIAS) + ".thumbnail." + extension)

jpg = re.compile(".*\.(jpg|jpeg)", re.IGNORECASE)
thumbnailer(".", jpg)

Best regards,

More information about the Python-list mailing list