views:

71

answers:

2

Hey guys,

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

A: 

How about running tiffcp with subprocess to convert to LZW (-c lzw switch), then process normally with pylibtiff? There are Windows builds of tiffcp lying around on the web. Not exactly Python-native solution, but still...

Constantin
I tried tiffcp, while it converted fine, the image still didn't load properly...I didn't know you could convert to lzw though...I'll give it a try...can you give me the syntax??
tylerthemiler
@tylerthemiler, no special syntax, just `tiffcp -c lzw from.tiff to.tiff`. And msw's answer has example of `subprocess` usage.
Constantin
A: 

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