regular expression concatenation with strings

O

oscartheduck

Hi folks,

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.



Here's the script:

---

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

size = 128, 128

# takes an extension (e.g. jpg, png, gif, tiff) as argument
def thumbnailer(extension):
#glob the directory the script is in for files of the type
foo.extension
for picture in glob.glob("*." + extension):
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)



---

And here's the error:

---

Traceback (most recent call last):
File "./thumbnail.py", line 19, in ?
thumbnailer(jpg)
File "./thumbnail.py", line 11, in thumbnailer
for picture in glob.glob("*." + extension):
TypeError: cannot concatenate 'str' and '_sre.SRE_Pattern' objects

---


It looks to me like the conversion to a regex object instead of a
plain string is screwing up the file glob + extension concatenation.
Is there a simple way to accomplish what I'm trying to do here and get
rid of that error?

Thanks!
 
J

Jacek Trzmiel

Hi,
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.
 
O

oscartheduck

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!



Hi,
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.
 
O

oscartheduck

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!





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!

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.
 
J

Jacek Trzmiel

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.

I haven't had PIL installed, so I just commented out im.* calls for
test. Just change:
im.save(file + ".thumbnail." + extension)
to:
im.save(file + ".thumbnail" + ext)

What was it you were trying to do to solve the
problem, though?

You can't use glob with regexps. Instead use os.listdir() to get
filenames and then rx.match() to find ones that does match the pattern.

Best regards,
Jacek.
 
O

oscartheduck

Got it:

#!/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" + ext)

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



The answer was sitting in front of my eyes. What is your code doing?
It looks like:

for $foo in [current working directory]
if $foo is a file
and foo's name matches the regex
 
O

oscartheduck

Oh dear. I just want to make it clear I wasn't trying to take credit
for the change of extension to ext. I genuinely worked it out
independently and then rushed back and posted about it, but you
definitely worked it out and wrote it up first!

Sorry! And thanks for the help!!



James
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top