472,994 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,994 software developers and data experts.

regular expressions eliminating filenames of type foo.thumbnail.jpg

Hi folks,

I'm trying to alter a program I posted about a few days ago. It
creates thumbnail images from master images. Nice and simple.

To make sure I can match all variations in spelling of jpeg, and
different cases, I'm using regular expressions.
The code is currently:

-----

#!/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 problem is this. This code outputs foo.thumbnail.jpg when ran, and
when ran again it creates foo.thumbnail.thumbnail.jpg and so on,
filling a directory.

The obvious solution is to filter out any name that contains the term
"thumbnail", which I can once again do with a regular expression. My
problem is the construction of this expression.

The relevant page in the tutorial docs is: http://docs.python.org/lib/re-syntax.html

It lists (?<!...) as the proper syntax, with the example given being
>>m = re.search('(?<!abc)def', 'abcdef')

I tried adding something like that to my original regex, but it added
a third argument, which re.compile can't accept.

jpg = re.compile("(?<!thumbnail).*\.(jpg|jpeg)", ".*\.(jpg|jpeg)",
re.IGNORECASE)

I tried it with re.search instead and received a lot of errors.
So I tried this:

jpg = re.compile(".*\.(jpg|jpeg)", re.IGNORECASE)
jpg = re.compile("(?<!*thumbnail).jpg", "jpg")
thumbnailer(".", jpg)
Two assignments, but I receive more errors telling me this:

[james@devil ~/pictures]$ ./thumbnail.2.py
Traceback (most recent call last):
File "./thumbnail.2.py", line 15, in ?
jpg = re.search("(?<!*thumbnail).jpg", "jpg")
File "/usr/local/lib/python2.4/sre.py", line 134, in search
return _compile(pattern, flags).search(string)
File "/usr/local/lib/python2.4/sre.py", line 227, in _compile
raise error, v # invalid expression
sre_constants.error: nothing to repeat


--------------------------------------------
I'm stuck as to where to go forwards from here. The code which
produced the above error is:

---------
#!/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)
jpg = re.search("(?<!*thumbnail).jpg", "jpg")
thumbnailer(".", jpg)
png = re.compile(".*\.png", re.IGNORECASE)
thumbnailer(".", png)
gif = re.compile(".*\.gif", re.IGNORECASE)
thumbnailer(".", gif)

---------
I'd like to know where I can find more information about regexs and
how to think with them, as well as some idea of the solution to this
problem. As it stands, I can solve it with a simple os.system call and
allow my OS to do the hard work, but I'd like the code to be portable.

Jun 25 '07 #1
7 2302
Why not ditch regular expressions altogether for this problem?

[ p for p in os.listdir(dir)
if os.path.isfile(os.path.join(dir,p))
and p.lower().find('.thumbnail.')==-1 ]

Jun 25 '07 #2
On Jun 25, 1:00 pm, Justin Ezequiel <justin.mailingli...@gmail.com>
wrote:
[ p for p in os.listdir(dir)
if os.path.isfile(os.path.join(dir,p))
and p.lower().find('.thumbnail.')==-1 ]
if you really want a regexp solution, the following seems to work
(?i)(?<!\.thumbnail)\.jpe?g$

download and experiment with Kodos (http://kodos.sourceforge.net/)

Jun 25 '07 #3
On Jun 24, 10:00 pm, Justin Ezequiel <justin.mailingli...@gmail.com>
wrote:
Why not ditch regular expressions altogether for this problem?

[ p for p in os.listdir(dir)
if os.path.isfile(os.path.join(dir,p))
and p.lower().find('.thumbnail.')==-1 ]
I like `and '.thumbnail.' not in p]` as a better ending. :)

~Sean

Jun 25 '07 #4
I eventually went with:

#!/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) if 'thumbnail' not in 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)
Thanks for the help!

Jun 25 '07 #5
Well, darn.

I just discovered that the computer this is going to run on only has
python version 2.2 installed on it, which apparently doesn't support
checking a whole word, but instead checks a single letter against
other single letters. 2.4 and, presumably 2.5 though I've actually not
used it much, has such huge leaps forwards relative to 2.2 that it's
frightening thinking about where the language is going.

But for now, I'm going to have to work on this problem again from
scratch, it seems.

On Jun 25, 3:41 pm, oscartheduck <oscarthed...@gmail.comwrote:
I eventually went with:

#!/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) if 'thumbnail' not in 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)

Thanks for the help!

Jun 25 '07 #6
En Mon, 25 Jun 2007 19:52:32 -0300, oscartheduck <os**********@gmail.com>
escribió:
Well, darn.

I just discovered that the computer this is going to run on only has
python version 2.2 installed on it, which apparently doesn't support
checking a whole word, but instead checks a single letter against
other single letters. 2.4 and, presumably 2.5 though I've actually not
used it much, has such huge leaps forwards relative to 2.2 that it's
frightening thinking about where the language is going.

But for now, I'm going to have to work on this problem again from
scratch, it seems.
You can still use a regular expression:

thumbnailRx = re.compile(r"\.thumbnail\.")

for picture in [... and filenameRx.match(p) and not thumbnailRx.search(p)]:
>>
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) if 'thumbnail' not in p]:
file, ext = os.path.splitext(picture)
--
Gabriel Genellina

Jun 26 '07 #7
On Jun 25, 2:41 pm, oscartheduck <oscarthed...@gmail.comwrote:
I eventually went with:

#!/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) if 'thumbnail' not in p]:
file, ext = os.path.splitext(picture)

(snipped)

Or, one can forego regular expressions:

prefix = '.thumbnail'
for p in os.listdir(dir):
root, ext = os.path.splitext(p)
if not os.path.isfile(os.path.join(dir, p)) \
or ext.lower() not in ('.jpg', '.jpeg') \
or root[-10:].lower() == prefix:
continue
if os.path.isfile(os.path.join(dir, "%s%s%s" % (root, prefix,
ext))):
print "A thumbnail of %s already exists" % p
else:
print "Making a thumbnail of %s" % os.path.join(dir, "%s%s%s"
%
(root, prefix, ext))
--
Hope this helps,
Steven

Jun 26 '07 #8

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

Similar topics

1
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...
6
by: Scott Zuyderduyn | last post by:
Hi all, I've started planning a software project that utilizes XML to store a lot of different input from the user. Regular expressions comprise a portion of this input. I could store a regex...
7
by: Patient Guy | last post by:
Coding patterns for regular expressions is completely unintuitive, as far as I can see. I have been trying to write script that produces an array of attribute components within an HTML element. ...
2
by: Sehboo | last post by:
Hi, I have several regular expressions that I need to run against documents. Is it possible to combine several expressions in one expression in Regex object. So that it is faster, or will I...
4
by: Együd Csaba | last post by:
Hi All, I'd like to "compress" the following two filter expressions into one - assuming that it makes sense regarding query execution performance. .... where (adate LIKE "2004.01.10 __:30" or...
7
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...
25
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...
3
by: Chris | last post by:
Hi everyone, I'm trying to parse through the contents of some text files with regular expressions, but am new to regular expressions and how to use them in VB.net. I'm pretty sure that the...
6
by: oscartheduck | last post by:
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...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.