regular expression concatenation with strings

oscartheduck oscartheduck at gmail.com
Fri Jun 22 17:23:11 EDT 2007



Shoot, I think I realised what I'm doing wrong.

Let me write some code to address this, but I'm almost certain that
the error is that I'm attempting to save an image with a regular
expression, which is by nature fluid, tacked on to its ass after
the .thumbnail. Which, now I look at your code, you address implicitly
by suggesting that the extension be a different variable.


Thanks!





On Jun 22, 3:10 pm, oscartheduck <oscarthed... at gmail.com> wrote:
> 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