batch tiff to jpeg conversion script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rtilley@vt.edu

    #1

    batch tiff to jpeg conversion script

    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)

  • Larry Bates

    #2
    Re: batch tiff to jpeg conversion script

    rtilley@vt.edu wrote:[color=blue]
    > 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)
    >[/color]
    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

    Comment

    • Peter Hansen

      #3
      Re: batch tiff to jpeg conversion script

      rtilley@vt.edu wrote:[color=blue]
      > Hope it's not inappropriate to post this here.
      >
      > Could someone critique my code?[/color]
      [snip][color=blue]
      > im.save(outfile , "JPEG", quality=100)[/color]

      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

      Comment

      • rtilley@vt.edu

        #4
        Re: batch tiff to jpeg conversion script

        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!

        Comment

        • rtilley@vt.edu

          #5
          Re: batch tiff to jpeg conversion script

          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!

          Comment

          • Martin Miller

            #6
            Re: batch tiff to jpeg conversion script

            rtilley@vt.edu wrote:[color=blue]
            > 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![/color]

            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

            Comment

            • Peter Hansen

              #7
              Re: batch tiff to jpeg conversion script

              Martin Miller wrote:[color=blue]
              > rtilley@vt.edu wrote:
              >[color=green]
              >>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![/color]
              >
              >
              > Allow me interject two observations:
              >
              > 1) You should tell the guy using Photoshop what Peter pointed out
              > regarding the Jpeg Quality setting.[/color]

              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

              Comment

              • Peter Hansen

                #8
                Re: batch tiff to jpeg conversion script

                rtilley@vt.edu wrote:[color=blue]
                > 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![/color]

                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

                Comment

                • rtilley@vt.edu

                  #9
                  Re: batch tiff to jpeg conversion script

                  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()

                  Comment

                  • Martin Miller

                    #10
                    Re: batch tiff to jpeg conversion script

                    rtilley@vt.edu wrote:[color=blue]
                    > 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()[/color]

                    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

                    Comment

                    • William Park

                      #11
                      Re: batch tiff to jpeg conversion script

                      rtilley@vt.edu <rtilley@vt.edu > wrote:[color=blue]
                      > 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![/color]

                      At the risk of being flamed... Have you tried ImageMagick utilities.
                      For example,
                      man convert

                      --
                      William Park <opengeometry@y ahoo.ca>, Toronto, Canada
                      ThinFlash: Linux thin-client on USB key (flash) drive

                      BashDiff: Super Bash shell
                      Compare the best free open source Software Development Software at SourceForge. Free, secure and fast Software Development Software downloads from the largest Open Source applications and software directory

                      Comment

                      Working...