#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25

    tga to jpg with python


    Does python have any modules that can open a tga file, then once open i can save the same file as a jpg file? If there is just point me in the right direction, i will figure out how i want to use it.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    10
    Originally Posted by CyBerHigh
    Does python have any modules that can open a tga file, then once open i can save the same file as a jpg file? If there is just point me in the right direction, i will figure out how i want to use it.
    Try Python Imaging Library.

    --OH.
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    611
    Rep Power
    65

    Smile


    Yes, you can easily do it with PIL, but it hasn't been updated for Python version 2.4 yet!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    thanks guys, that was perfect here is what i did

    Code:
    import os, sys
    import Image
    
    dirList = os.listdir("c:\\img\\")
    counter = 0
    end = len(dirList)
    
    while counter < end:
        outfile = dirList[counter] + ".jpg"
        print "opening %s" % dirList[counter]
        try:
            Image.open("c:\\img\\%s" % dirList[counter] ).save("c:\\jpg\\%s" % outfile)
            print "saving as %s" % outfile
        except IOError:
    	print "cannot convert", dirList[counter]
    
    
        counter += 1
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    I apologise, but I just have to rewrite your "C code that runs in Python" into "Python code"

    Code:
    from glob import glob
    import Image
    
    for picture in glob("c:\\img\\*.tga"):
        outfile = os.path.splitext(picture)[0] + ".jpg"
    
        try:
            print "Converting: ", picture
            Image.open(picture).save(outfile)
        except IOError:
    	print "    * Conversion failed for this file."
    - Iteration over a list implicitly rather than doing it yourself with a counter
    - Three lots of "c:\\img\\" could get annoying to change and looks a bit untidy.
    - os.path.splitext will remove the current extension, so you wont end up with "file001.tga.jpg", etc.
    - glob will both: get only .tga files, and include the full pathname in the list of files it returns, so removing the need for: "c:\\img\\%s " % filename.
    - Adjusted the messages printed. You know it's converting .tga to .jpg, so why print

    Code:
    Opening A
    Saving as A.jpg
    Failed to convert A
    for each file, when you could have:

    Code:
    Converting A
        * Conversion Failed
    (Errors stand out more, and the filename duplication is reduced. Depending on the future of this, you could have it work with optparse so you could choose to see only failed files. (Because those are the only ones you really care to hear about)).
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    Well, there is no feture of this code. One of my friends asked me if i knew of any programs that could convert tga files to jpgs without having to hand open all of them and then resave them. There where also bmp files in the mix also, so i just have it open any file type, i just told him to make sure there where no other files types in there, besides images. I didn't know about the splittext, that would have been nice, and thanks for the pointer with the glob, I didn't not know about that.
  12. #7
  13. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    611
    Rep Power
    65

    Smile


    Don't forget the beginners! Newbees like me can learn a lot from both coding styles. Great thread, thank you very much!

    BTW, you can replace the \\ with /
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    10
    Originally Posted by sfb
    Code:
    from glob import glob
    [...Pythonic code]
    _Much_ better ;-)

    -OH.

IMN logo majestic logo threadwatch logo seochat tools logo