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(pa th):
for root, dirs, files in os.walk(path):
for f in files:
if os.path.splitex t(os.path.join( root,f))[1].lower() ==
".tif":
# If a jpeg is already present. Don't do anything.
if
os.path.isfile( os.path.splitex t(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.splitex t(os.path.join( root,f))[0]
+ ".jpg"
try:
im = Image.open(os.p ath.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(pa th) 10 11060 rt*****@vt.edu 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(pa th):
for root, dirs, files in os.walk(path): for f in files: if os.path.splitex t(os.path.join( root,f))[1].lower() == ".tif":
# If a jpeg is already present. Don't do anything. if os.path.isfile( os.path.splitex t(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.splitex t(os.path.join( root,f))[0] + ".jpg" try: im = Image.open(os.p ath.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(pa th)
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().en dswith('.tif')]:
# If a jpeg is already present. Don't do anything.
filename, extension=f.spl it('.')
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.p ath.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 rt*****@vt.edu 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
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!
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! rt*****@vt.edu 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
Martin Miller wrote: rt*****@vt.edu 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 rt*****@vt.edu 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.splitex t() which will do the right thing in any case (even
on names without extensions!).
-Peter
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() rt*****@vt.edu wrote: 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()
Yes, in the sense that there is an "Include All Subfolders" option for
batch operation source files that recursively locates input files.
However, at least in the version I have (CS), there is no obvious way
to get it to recreate the input folder hierarchy with a different root
folder specified as the destination (all the output files get put in
single folder specified) -- something that could be fairly easily
accomplished using a Python script.
Since in this case you are doing file *conversions*, the output files
will have a different extension than the orginals, and can therefore
exist in the same folders (assuming there's enough disk space). This
makes it possible to record a "Save As" command in the Action which to
simply save the converted image back into the same folder as the
orginal in a different format, thus preserving the file & folder
layout.
Sorry, but I feel any more detail on the process would be getting way
too off-topic for this newsgroup. Feel free to contact me directly if
you would like to discuss in more detail how to do this sort of thing
from within Photoshop.
Best,
-Martin This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Dominic |
last post by:
Hi guys,
In .NET, how can I convert a HTML file to TIFF efficiently?
One possible way is that I can first use word automatation to load the
HTML up and then print it to TIFF. Is that right? However, even if it
is technically feasible, it can take long time to do the conversion,
especially the HTML is complicated.
Is there any other more efficient way? Thanks!
|
by: Ron |
last post by:
I need to take a large Tiff file composed of several scanned pages and
convert it to several individual Jpegs. I guess I will have to write a
component myself, doing the conversion on the byte level, but first I need a
map of the byte layout for a Tiff file and the algorithm to do the
conversion (or convert to BMP first if necessary). Can anyone here point me
in the right direction?
This might not be the best place to ask, if anyone can...
|
by: Nicolas Guilhot |
last post by:
Hi all !
I have a multi-page Tiff image file that I want to convert to PDF. To do so
I am using iText library. The conversion is working, but the code execution
is very different according to wich
iTextSharp.text.Image.getInstance(...) signature I am using :
- using code 1 below, the conversion is fast enough but the resulting PDF
file is too big (1 817ko sample Tiff file is converted in less than 30
seconds to a 2 764ko PDF file)
-...
|
by: 246C57AE-40DD-4d6b-9E8D-B0F5757BB2A8 |
last post by:
Hi.
Why can't I read TIFFs compressed by JPEG using GDI+ Image class? I always
get out of memory exception.
Thanx.
|
by: T. Davis |
last post by:
In C#, I am able to successfully stream a TIFF image that comes from a
BLOB field in a database, save it to file, then convert the pages
within TIFF file into jpegs (using GDI+) and display on the web (using
ASP.NET).
However, when I generate the Image object using FromStream (passing in
the MemoryStream containing image bytes), an exception of "A generic
error occurred in GDI+" is thrown when performing the conversion/save
for display....
| |
by: =?Utf-8?B?R3VpbGxlcm1vIEppbWVuZXo=?= |
last post by:
Hi....
Please help me with this case. I need to show an image .TIFF in a .aspx web
page. The web site is developer on Visual Studio 2005. The image control no
support this image format, that control i can use to show this format???
Thanks.
|
by: a.mustaq |
last post by:
Hi All,
In my application, I have to display tiff images
stored on server. Please help me in acheaving this.
With Regards,
Mustaq Ahmed.A
|
by: vasikaran |
last post by:
Hi ,
i hava one ftp script in batch file programming ,
ftp script is working fine..
but my is i dont know how much files and what are files avaiulable in remote folder ,,,,
if any errors or failures occurring in ftp process how to trace failure files...
everything in batch file script only...
this is my ftp script ...
@ echo off
|
by: mndprasad |
last post by:
hi all
i am doing a project in java where i need to convert 10 jpeg images into a single tiff image..conversion of single jpeg image to single tiff is happening but embedding all the 10 jpeg images into a single tiff is not happening..i hav tried out with single image conversion..
My code is
***********************
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |