Search code examples
pythoncompressiontiffimagingimage-formats

What is the best way to load a CCITT T.3 compressed tiff using python?


I am trying to load a CCITT T.3 compressed tiff into python, and get the pixel matrix from it. It should just be a logical matrix.

I have tried using pylibtiff and PIL, but when I load it with them, the matrix it returns is empty. I have read in a lot of places that these two tools support loading CCITT but not accessing the pixels.

I am open to converting the image, as long as I can get the logical matrix from it and do it in python code. The crazy thing is is that if I open one of my images in paint, save it without altering it, then try to load it with pylibtiff, it works. Paint re-compresses it to the LZW compression.

So I guess my real question is: Is there a way to either natively load CCITT images to matricies or convert the images to LZW using python??

Thanks,

tylerthemiler


Solution

  • It seems the best way is to not use Python entirely but lean on netpbm:

    import Image
    import ImageFile
    import subprocess
    
    tiff = 'test.tiff'
    im = Image.open(tiff)
    print 'size', im.size
    try:
        print 'extrema', im.getextrema()
    except IOError as e:
        print 'help!', e, '\n'
    
    print 'I Get by with a Little Help from my Friends'
    pbm_proc = subprocess.Popen(['tifftopnm', tiff],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
    (pbm_data, pbm_error) = pbm_proc.communicate()
    ifp = ImageFile.Parser()
    ifp.feed(pbm_data)
    im = ifp.close()
    print 'conversion message', pbm_error,
    print 'extrema', im.getextrema()
    print 'size', im.size
    # houston: we have an image
    im.show()
    

    Seems to do the trick:

    $ python g3fax.py 
    size (1728, 2156)
    extrema help! decoder group3 not available 
    
    I Get by with a Little Help from my Friends
    conversion message tifftopnm: writing PBM file
    extrema (0, 255)
    size (1728, 2156)