473,544 Members | 143 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

regular expression concatenation with strings

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("j pg),
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(ext ension):
#glob the directory the script is in for files of the type
foo.extension
for picture in glob.glob("*." + extension):
file, ext = os.path.splitex t(picture)
im = Image.open (picture)
im.thumbnail(si ze, 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_Patte rn' 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!

Jun 22 '07 #1
6 5628
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("j pg),
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(di r,p)) and filenameRx.matc h(p) ]:
file, ext = os.path.splitex t(picture)
im = Image.open (picture)
im.thumbnail(si ze, Image.ANTIALIAS )
im.save(file + ".thumbnail ." + extension)
jpg = re.compile(".*\ .(jpg|jpeg)", re.IGNORECASE)
thumbnailer("." , jpg)
Best regards,
Jacek.

Jun 22 '07 #2
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...@hot.plwro te:
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("j pg),
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(di r,p)) and filenameRx.matc h(p) ]:
file, ext = os.path.splitex t(picture)
im = Image.open (picture)
im.thumbnail(si ze, Image.ANTIALIAS )
im.save(file + ".thumbnail ." + extension)

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

Best regards,
Jacek.

Jun 22 '07 #3


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...@g mail.comwrote:
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...@hot.plwro te:
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("j pg),
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(di r,p)) and filenameRx.matc h(p) ]:
file, ext = os.path.splitex t(picture)
im = Image.open (picture)
im.thumbnail(si ze, Image.ANTIALIAS )
im.save(file + ".thumbnail ." + extension)
jpg = re.compile(".*\ .(jpg|jpeg)", re.IGNORECASE)
thumbnailer("." , jpg)
Best regards,
Jacek.

Jun 22 '07 #4
Hi,

oscartheduck wrote:
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.

Jun 22 '07 #5
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.matc h(p) ]:
file, ext = os.path.splitex t(picture)
im = Image.open (picture)
im.thumbnail(si ze, 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

Jun 22 '07 #6
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

On Jun 22, 4:07 pm, oscartheduck <oscarthed...@g mail.comwrote:
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.matc h(p) ]:
file, ext = os.path.splitex t(picture)
im = Image.open (picture)
im.thumbnail(si ze, 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

Jun 22 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2192
by: pekka niiranen | last post by:
Hi there, I have perl script that uses dynamically constructed regular in this way: ------perl code starts ---- $result ""; $key = AAA\?01; $key = quotemeta $key; $line = " s^\?AAA\?01^BBB^g; #Comment "
1
4156
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make regular expressions easier to create and use (and in my experience as a regular expression user, it makes them MUCH easier to create and use.) ...
4
3210
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go over each document, find out if it contains a header and/or a footer and extract only the main content part. The headers and the footers have no...
2
3005
by: Brian Kitt | last post by:
I have a process where I do some minimal reformating on a TAB delimited document to prepare for DTS load. This process has been running fine, but I recently made a change. I have a Full Text index on one column, and punctuation in the column was causing some problems down the line. This column is used only for full text indexing, and...
7
3798
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I want to avoid that. My question here is if there is a way to pass either a memory stream or array of "find", "replace" expressions or any other way...
3
2553
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular expression. ^(.+?) uses (?!a spoon)\.$
25
5132
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART (CONDUCTION DEFECT) 37.33/2 HEART (CONDUCTION DEFECT) WITH CATHETER 37.34/2 " the expression is "HEART (CONDUCTION DEFECT)". How do I gain access...
34
2616
by: Larry Hastings | last post by:
This is such a long posting that I've broken it out into sections. Note that while developing this patch I discovered a Subtle Bug in CPython, which I have discussed in its own section below. ____________ THE OVERVIEW I don't remember where I picked it up, but I remember reading years ago that the simple, obvious Python approach for...
4
1622
by: carlos | last post by:
I am working on a regular expression validation for my search page. What I have so far works for most cases, but I would like to fine tune it some. I am new to regular expressions, and I do not have the time to read up some more on it. Can someone help? What I would like to do is allow words to be parsed using quotes. However, they can also...
0
7429
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7368
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7614
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7777
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5916
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3420
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3414
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
990
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.