![](/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.