Converting an image instance (file) to an array (python)
Question: I have an image sequence and I am trying to run a script on a specific frame.
I need to switch to this frame and convert it to an array. However, I am
unable to as the frame is an instance `<TiffImagePlugin.TiffImageFile image
mode=I;16 size=512x512 at 0x104A0C998>`. How can I convert this instance to an
array? I have already used numpy.array and it does not work. Thank you!

    
    
    prot=Image.open("F23BN.tif")
    for frame in ImageSequence.Iterator(dna):
        if frame==16:
            frame.convert('L')
            print frame.mode, frame.format #I checked the format and it is still in the Instance format
    


Answer: As I understand your question, you are trying to get access to the binary data
contained in a tiff image. As Dav1d suggested you can do it in PIL. I tested
the following in python 2.6.5

    
    
    import Image
    import numpy
    
    im = Image.open('Tiff.tif')
    
    imarray = numpy.array(im)
    print imarray.shape, im.size   #these agree
    

For a more difficult way to do this, you can open the file just as you would
any other file. In the code snippet, I assume that your file isn't too big to
just load into memory all at once.

    
    
    im = open('Tiff.tif','r')
    image_data = im.read()
    im.close()
    
    #lets look at the first few characters
    for char in image_data[:64]:  
        print ord(char),  #need to use ord so we can see the number
    

With the data in the string image_data, you are free to turn it into any other
data type you wish. However, this may not be useful right away. First of all
the data is binary. You will need to decipher it using the tiff specification.
For example, the first 8 bytes are a header.

More details: <http://partners.adobe.com/public/developer/en/tiff/TIFF6.pdf>

