regular expression concatenation with strings

oscartheduck oscartheduck at gmail.com
Fri Jun 22 23:10:37 CEST 2007


Hi,

I noticed a small error in the code (you referenced extension, which
you had renamed to filenameRx), and when I corrected it I received the
original error again. What was it you were trying to do to solve the
problem, though?

Thanks!



On Jun 22, 2:41 pm, Jacek Trzmiel <s... at hot.pl> wrote:
> Hi,
>
> 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 = Image.open (picture)
>          im.thumbnail(size, Image.ANTIALIAS)
>          im.save(file + ".thumbnail." + extension)
>
> jpg = re.compile(".*\.(jpg|jpeg)", re.IGNORECASE)
> thumbnailer(".", jpg)
>
> Best regards,
> Jacek.







More information about the Python-list mailing list