How to read a large image in chunks in python?

梦想与她 提交于 2019-12-08 04:33:40

问题


I'm trying to compute the difference in pixel values of two images, but I'm running into memory problems because the images I have are quite large. Is there way in python that I can read an image lets say in 10x10 chunks at a time rather than try to read in the whole image? I was hoping to solve the memory problem by reading an image in small chunks, assigning those chunks to numpy arrays and then saving those numpy arrays using pytables for further processing. Any advice would be greatly appreciated.

Regards,

Berk


回答1:


You can use numpy.memmap and let the operating system decide which parts of the image file to page in or out of RAM. If you use 64-bit Python the virtual memory space is astronomic compared to the available RAM.




回答2:


If you have time to preprocess the images you can convert them to bitmap files (which will be large, not compressed) and then read particular sections of the file via offset as detailed here:

Load just part of an image in python

Conversion from any file type to bitmap can be done in Python with this code:

from PIL import Image
file_in = "inputCompressedImage.png"

img = Image.open(file_in)

file_out = "largeOutputFile.bmp"

img.save(file_out)


来源:https://stackoverflow.com/questions/21765647/how-to-read-a-large-image-in-chunks-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!