简体   繁体   中英

Can you loop through pixels in an image without loading the whole image?

I have some very large images. I don't want to load the whole image into memory, I just want to make a single pass through the image in row order. Is it possible to do this in Python/scipy?

EDIT: I'm using .PNG, but I could convert them to PPM, BMP or something else lossless.

GDAL (with Python bindings) offers some very good drivers for this. Although its a geospatial package, it works fine with BMP and PNG for example. This example show how to load a PNG row by row:

import gdal

# only loads the dataset
ds = gdal.Open('D:\\my_large_image.png')

# read 1 row at the time
for row in range(ds.RasterYSize):
    row_data = ds.ReadAsArray(0,row,ds.RasterXSize,1)

ds = None # this closes the file

It gives you a Numpy array as a result, so ready for procesing. You could write any result in a similar fashion.

print type(row_data)
<type 'numpy.ndarray'>

print row_data.shape
(3, 1, 763)

print row_data
[[[  0   0 255 ..., 230 230   0]]

 [[  0   0 252 ..., 232 233   0]]

 [[  0   0 252 ..., 232 233   0]]]

Installing a package specific for reading might be a bit overkill if PIL or something else can do it. But its a robust option, i have processed images of 30000*30000 pixels like this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM