473,320 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 2322
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
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.