![](/img/trans.png)
[英]how can I load a single tif image in parts into numpy array without loading the whole image into memory?
[英]Can you loop through pixels in an image without loading the whole image?
我有一些非常大的图像。 我不想将整个图像加载到内存中,我只想按行顺序对图像进行单次传递。 是否可以在Python / scipy中执行此操作?
编辑:我正在使用.PNG,但我可以将它们转换为PPM,BMP或其他无损的东西。
GDAL (使用Python绑定)为此提供了一些非常好的驱动程序。 虽然它是一个地理空间包,但它可以与BMP和PNG一起使用。 此示例显示如何逐行加载PNG:
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
它为您提供了一个Numpy数组,因此可以进行处理。 你可以用类似的方式写任何结果。
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]]]
如果PIL或其他东西能够做到这一点,安装专门用于阅读的包可能有点过分。 但它是一个强大的选项,我已经处理了像这样的30000 * 30000像素的图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.