Is there any way to read one image row/column into an array in Python?

3.1k views Asked by At

I've just translated some CT reconstruction software from IDL into Python, and this is my first experience ever with Python. The code works fine except that it's much, much slower. This is due, in part, to the fact that IDL allows me to save memory and time by reading in just one row of an image at a time, using the following:

image = read_tiff(filename, sub_rect = [0, slice, x, 1])

I need to read one row each from 1800 different projection images, but as far as I can tell I can only create an image array by reading in the entire image and then converting it to an array. Is there any trick to just read in one row from the start, since I don't need the other 2047 rows?

1

There are 1 answers

2
Christian K. On BEST ANSWER

It looks like tifffile.py by Christoph Gohlke (http://www.lfd.uci.edu/~gohlke/code/tifffile.py.html) can do the job.

from tiffile import TiffFile

with TiffFile('test.tiff') as tif:
    for page in tif:
        image = page.asarray(memmap=True)
        print image[0,:,:]

If I interpret the code correctly this will extract the first row of every page in the file without loading the whole file into memory (through numpy.memmap).