Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > batch tiff to jpeg conversion script

Reply
Thread Tools

batch tiff to jpeg conversion script

 
 
rtilley@vt.edu
Guest
Posts: n/a
 
      01-11-2006
Hope it's not inappropriate to post this here.

Could someone critique my code? I have no Python programmers in my
office to show this to. The script works OK, but should I do it
differently? I especially don't like how I check to see if jpegs exist.

The style may not be acceptable to some, but I'm concerned with
substance, not style. Is there a 'more appropriate' way to do this?

Thanks to all who take the time to give advice!

-----------------------------------------------------------------
import os
import os.path
#From PIL
import Image

def tiff_to_jpeg(path):

for root, dirs, files in os.walk(path):
for f in files:
if os.path.splitext(os.path.join(root,f))[1].lower() ==
".tif":

# If a jpeg is already present. Don't do anything.
if
os.path.isfile(os.path.splitext(os.path.join(root, f))[0] + ".jpg"):
print "A jpeg file already exists for %s" %f

# If a jpeg is *NOT* present, create one from the tiff.
else:
outfile = os.path.splitext(os.path.join(root,f))[0]
+ ".jpg"
try:
im = Image.open(os.path.join(root,f))
print "Generating jpeg for %s" %f
im.thumbnail(im.size)
im.save(outfile, "JPEG", quality=100)
except Exception, e:
print e

# Run Program
path = '.'
tiff_to_jpeg(path)

 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      01-11-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hope it's not inappropriate to post this here.
>
> Could someone critique my code? I have no Python programmers in my
> office to show this to. The script works OK, but should I do it
> differently? I especially don't like how I check to see if jpegs exist.
>
> The style may not be acceptable to some, but I'm concerned with
> substance, not style. Is there a 'more appropriate' way to do this?
>
> Thanks to all who take the time to give advice!
>
> -----------------------------------------------------------------
> import os
> import os.path
> #From PIL
> import Image
>
> def tiff_to_jpeg(path):
>
> for root, dirs, files in os.walk(path):
> for f in files:
> if os.path.splitext(os.path.join(root,f))[1].lower() ==
> ".tif":
>
> # If a jpeg is already present. Don't do anything.
> if
> os.path.isfile(os.path.splitext(os.path.join(root, f))[0] + ".jpg"):
> print "A jpeg file already exists for %s" %f
>
> # If a jpeg is *NOT* present, create one from the tiff.
> else:
> outfile = os.path.splitext(os.path.join(root,f))[0]
> + ".jpg"
> try:
> im = Image.open(os.path.join(root,f))
> print "Generating jpeg for %s" %f
> im.thumbnail(im.size)
> im.save(outfile, "JPEG", quality=100)
> except Exception, e:
> print e
>
> # Run Program
> path = '.'
> tiff_to_jpeg(path)
>

The methodology seems just fine. You may (or may not) find
the following code easier to read (not tested):

for f in [file for file in files if file.lower().endswith('.tif')]:
# If a jpeg is already present. Don't do anything.
filename, extension=f.split('.')
jpgfile="%s.jpg" % filename
jpgpath=os.path.join(root, jpgfile)
# If a jpeg is *NOT* present, create one from the tiff.
if not os.path.isfile(jpgpath):
try:
im = Image.open(os.path.join(root,f))
print "Generating jpeg for %s" % f
im.thumbnail(im.size)
im.save(jpgpath, "JPEG", quality=100)
except Exception, e:
print e

continue

print "A jpeg file already exists for %s" % f


This code:

1) only processess .tif files
2) simplifies things by eliminating the splitext methods and
slicing operations.
3) eliminates else branch

-Larry Bates
 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      01-11-2006
(E-Mail Removed) wrote:
> Hope it's not inappropriate to post this here.
>
> Could someone critique my code?

[snip]
> im.save(outfile, "JPEG", quality=100)


From an effbot posting on 13 Jul 2002:

'''JPEG quality 100 is overkill, btw -- it completely disables JPEG's
quantization stage, and "mainly of interest for experimental pur-
poses", according to the JPEG library documentation, which
continues:

"Quality values above about 95 are NOT recommended for
normal use; the compressed file size goes up dramatically
for hardly any gain in output image quality."

(full text below):

Should probably add something about this to the PIL docs...
'''

(As near as I can tell, so far, the last comment hasn't been followed
through on.)

-Peter

 
Reply With Quote
 
rtilley@vt.edu
Guest
Posts: n/a
 
      01-11-2006
Hi Peter. The guy who takes the pictures uses Photoshop to convert
tiffs to jpegs one by one. When he does a 'Maxium Quality' conversion
in Photoshop and I do a 100% quality conversion with Python and PIL,
the two converted files are almost identical and this is what he
wants... that's the only reason I'm using 100% quality. Thanks for the
info!

 
Reply With Quote
 
rtilley@vt.edu
Guest
Posts: n/a
 
      01-11-2006
Thanks for the example code Larry. It _is_ easier for me to read. I
like the way you split the file on '.' I may use that. Thanks again!

 
Reply With Quote
 
Martin Miller
Guest
Posts: n/a
 
      01-11-2006
(E-Mail Removed) wrote:
> Hi Peter. The guy who takes the pictures uses Photoshop to convert
> tiffs to jpegs one by one. When he does a 'Maxium Quality' conversion
> in Photoshop and I do a 100% quality conversion with Python and PIL,
> the two converted files are almost identical and this is what he
> wants... that's the only reason I'm using 100% quality. Thanks for the
> info!


Allow me interject two observations:

1) You should tell the guy using Photoshop what Peter pointed out
regarding the Jpeg Quality setting.

2) Although it wouldn't be as flexible as your Python script, it's
completely possible and fairly easy to automate such a conversion
within Photoshop using 'Actions', which are like recorded macros,
coupled with the Automate | Batch... submenu.

Best,
-Martin

 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      01-11-2006
(E-Mail Removed) wrote:
> Thanks for the example code Larry. It _is_ easier for me to read. I
> like the way you split the file on '.' I may use that. Thanks again!


Warning: that will fail on names with more than one "." in them. It's
generally best to use the provided tools for working with paths, in this
case os.path.splitext() which will do the right thing in any case (even
on names without extensions!).

-Peter

 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      01-11-2006
Martin Miller wrote:
> (E-Mail Removed) wrote:
>
>>Hi Peter. The guy who takes the pictures uses Photoshop to convert
>>tiffs to jpegs one by one. When he does a 'Maxium Quality' conversion
>>in Photoshop and I do a 100% quality conversion with Python and PIL,
>>the two converted files are almost identical and this is what he
>>wants... that's the only reason I'm using 100% quality. Thanks for the
>>info!

>
>
> Allow me interject two observations:
>
> 1) You should tell the guy using Photoshop what Peter pointed out
> regarding the Jpeg Quality setting.


Or consider using PNG files instead, which can do pretty decent lossless
compression, which might be what the guy really wants to do. I haven't
compared a 100% JPG with a PNG but it might be instructive.

-Peter

 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      01-12-2006
On 11 Jan 2006 13:42:21 -0800, "(E-Mail Removed)" <(E-Mail Removed)>
declaimed the following in comp.lang.python:

> Hi Peter. The guy who takes the pictures uses Photoshop to convert
> tiffs to jpegs one by one. When he does a 'Maxium Quality' conversion
> in Photoshop and I do a 100% quality conversion with Python and PIL,


Current versions of PhotoShop have a batch convert capability...
Though it does take some gaming to get the options on the action/command
to work as expected.
--
> ================================================== ============ <
> (E-Mail Removed) | Wulfraed Dennis Lee Bieber KD6MOG <
> (E-Mail Removed) | Bestiaria Support Staff <
> ================================================== ============ <
> Home Page: <http://www.dm.net/~wulfraed/> <
> Overflow Page: <http://wlfraed.home.netcom.com/> <

 
Reply With Quote
 
rtilley@vt.edu
Guest
Posts: n/a
 
      01-12-2006
Just curious... is PhotoShop _really_ recursive? We have dozens of
levels of sub-folders where the pics have been sorted and thousands of
pics. That's one reason I used os.walk()

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Library for Images Conversion to PSD, BMP, JPEG, PNG, TIFF or GIF sherazam Java 0 09-26-2012 10:14 AM
JAI Problem - TIFF Image to JPEG conversion surendra Java 1 06-21-2008 07:39 AM
Tool for converting 12-bit TIFF images to 16-bit TIFF-images? Peter Frank Digital Photography 11 12-13-2004 02:41 AM
jpeg to tiff and back to tiff ARosenblat Digital Photography 6 01-09-2004 08:53 PM
TiFF to JPEG conversion Buy-N-Sell Digital Photography 16 12-11-2003 04:57 PM



Advertisments