Tiff Image Reader/writer

Discussion in 'Python' started by PyPK, Jun 13, 2005.

  1. PyPK

    PyPK Guest

    Hi I am looking for a simple tiff Image reader/writer in python.Can
    anyone point me to the right one.
     
    PyPK, Jun 13, 2005
    #1
    1. Advertisements

  2. PyPK

    Dan Sommers Guest

    I don't know what your definition of "simple" is, but check out the
    Python Imaging Library (PIL) at effbot.org.

    Regards,
    Dan
     
    Dan Sommers, Jun 13, 2005
    #2
    1. Advertisements

  3. PyPK

    PyPK Guest

    Is there any package out there which handles Tiff Images other than PIL
    or ImageMagic .
     
    PyPK, Jun 13, 2005
    #3
  4. What sort of things do you want to do with the TIFFs? How
    heavy-weight or light-weight are you interested in? For heavy-weight
    there are:

    - wxPython will do a bunch of tiff reading, and some image processing
    http://www.wxpython.org

    - GDAL
    - ITK Insight Toolkit from Kitware (wicked heavy-weight)
    http://www.itk.org

    -Jim
     
    James Carroll, Jun 13, 2005
    #4
  5. PyPK

    PyPK Guest

    nothing fancy. I just want to be able to read a tiff image, get pixel
    values, write back to a tiff file.
     
    PyPK, Jun 13, 2005
    #5
  6. PyPK

    Robert Kern Guest

    [An aside: please quote the message you are replying to.]

    Why doesn't the PIL satisfy this need? Or are you just collecting a list
    of packages with this capability?

    --
    Robert Kern


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
     
    Robert Kern, Jun 13, 2005
    #6
  7. PyPK

    PyPK Guest

    One reason why I don't want to use PIL is it seems very slow for tiff
    images of very large sizes(2400x4800). So I am looking for a better
    tool than does the right job faster.
     
    PyPK, Jun 13, 2005
    #7
  8. PyPK

    Robert Kern Guest

    This isn't fast enough?

    In [8]: %time img2 = Image.open('foo.tiff')
    CPU times: user 0.00 s, sys: 0.01 s, total: 0.01 s
    Wall time: 0.03

    In [9]: img2.size
    Out[9]: (2400, 4800)

    --
    Robert Kern


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
     
    Robert Kern, Jun 13, 2005
    #8
  9. PyPK

    PyPK Guest

    When i try this i get this error:

    import ImageTraceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "Image.py", line 858, in getpixel
    self.load()
    File "/usr/local/lib/python2.4/site-packages/PIL/ImageFile.py", line
    180, in load
    d = Image._getdecoder(self.mode, d, a, self.decoderconfig)
    File "Image.py", line 328, in _getdecoder
    raise IOError("decoder %s not available" % decoder_name)
    IOError: decoder group4 not available
     
    PyPK, Jun 13, 2005
    #9
  10. so why doesn't PIL or ImageMagick work for you?

    here's a minimal PIL version:

    from PIL import Image

    im = Image.open("myfile.tiff")
    value = im.getpixel((12, 34))
    im.save("other.tiff")

    </F>
     
    Fredrik Lundh, Jun 14, 2005
    #10
  11. PyPK

    PyPK Guest

    I get a decoder error when i do a get pixel on the Image

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "Image.py", line 858, in getpixel
    self.load()
    File "/usr/local/lib/python2.4/site-packages/PIL/ImageFile.py", line
    180, in load
    d = Image._getdecoder(self.mode, d, a, self.decoderconfig)
    File "Image.py", line 328, in _getdecoder
    raise IOError("decoder %s not available" % decoder_name)
    IOError: decoder group4 not available
     
    PyPK, Jun 14, 2005
    #11
  12. Hmm... that's unfortunate.

    What platform are you on? If Windows, then I believe that PIL is
    statically linked against LibTIFF and that particular libtiff wasn't
    compiled with certain options (CCITT formats or something.) (in 1999
    that was true, I found a post from Fred here:
    http://mail.python.org/pipermail/image-sig/1999-June/000755.html)

    If it's a one-time thing, there are ways of converting the TIFF to a
    different encoding. I use the libtiff tifftools command tiffcp:

    tiffcp -c none infile.tif out file.tif

    which will decode into a non-compressed tiff.

    Otherwise, you might be out of luck (or have to change some of the
    libtiff options.)

    If you put your tiff file somewhere where I can download it, I'll try
    reading it with the alternatives that I have on my system and let you
    know what works...

    -Jim
     
    James Carroll, Jun 14, 2005
    #12
  13. PIL doesn't support CCITT encodings out of the box. the following
    patch might help:

    http://mail.python.org/pipermail/image-sig/2003-July/002354.html

    (unfortunately, this currently depends on libtiff internals, which is
    why it wasn't added to 1.1.5)

    an alternative solution is to pipe your files through tiffcp (see James
    mail for details).

    </F>
     
    Fredrik Lundh, Jun 14, 2005
    #13
  14. It's fast enough to open the file and read the meta-data. The OP wants to
    decode the actual pixels.

    Ciao,
    Marc 'BlackJack' Rintsch
     
    Marc 'BlackJack' Rintsch, Jun 15, 2005
    #14
  15. PyPK

    Robert Kern Guest

    Okay.

    In [12]: %time d = img.getdata()
    CPU times: user 0.31 s, sys: 1.43 s, total: 1.73 s
    Wall time: 6.19

    --
    Robert Kern


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
     
    Robert Kern, Jun 15, 2005
    #15
  16. What do you mean by decode the pixels? If there's some image
    processing that needs to be done, or if you want to view, brighten, or
    print or something, then there are ways of doing it that will be as
    fast as can be. If stepping through the pixels to do your own math
    is what you want, then maybe something that puts the pixels in
    numarray / scipy / etc. form is what would work for you.

    Reading a large image is going to take some time no matter what
    language or tool you use... How long does it take a viewer program to
    open and display your tiff image?

    How fast is the following for your image? (you'll need wxPython)

    import wx

    wx.InitAllImageHandlers()


    # time the following line
    image = wx.Image('yourfilename.tif')
    print "the image is %ix%i" % (image.GetWidth(), image.GetHeight())
    print "the greenness pixel at 10,10 is", image.GetGreen(10,10)

    wxWidgets uses libtiff directly, so it will be about as fast as can
    be, but wxWidgets does assume that the image is RGB for most
    operations.

    The Kitware ITK (Insight Toolkit) can read lots of variations of
    multi-tiff images, and do tons of different kinds of manipulation on
    them... the only downside is its footprint and learning curve.


    -Jim

     
    James Carroll, Jun 16, 2005
    #16
  17. PyPK

    Robert Kern Guest

    And for comparison:

    In [2]: f = open('foo.tiff')

    In [3]: %time s = f.read()
    CPU times: user 0.04 s, sys: 0.28 s, total: 0.32 s
    Wall time: 2.40

    Of course, it's all a moot point since the OP actually has a different
    problem with PIL, not its speed.

    --
    Robert Kern


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
     
    Robert Kern, Jun 16, 2005
    #17
  18. I did a test with wxPython 2.6.1 on Windows. I created a G4 TIFF
    image that was 4400 x 3599 big, and the following code took under a
    half second.

    import wx
    import time

    def readImage(filename):
    img = wx.Image(filename)
    w = img.GetWidth()
    h = img.GetHeight()
    value = img.GetGreen(w - 10, h - 10)
    return value

    image = "D:\\horizLines1g4.tif"

    t0 = time.clock()
    value = readImage(image)
    print time.clock() - t0, "seconds process time"
    print "The value in the lower right hand corner is %i" % value


    This prints:
    0.488175452467 seconds process time
    The value in the lower right hand corner is 255

    so it is treating it as RGB, but I bet that's not a problem.

    -Jim
     
    James Carroll, Jun 16, 2005
    #18
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.