简体   繁体   English

如何在python中读取大块图像?

[英]How to read a large image in chunks in python?

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? python中有没有办法我可以一次读取10x10的图像而不是尝试读取整个图像? 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. 我希望通过读取小块图像来解决内存问题,将这些块分配给numpy数组,然后使用pytables保存这些numpy数组以进行进一步处理。 Any advice would be greatly appreciated. 任何建议将不胜感激。

Regards, 问候,

Berk 伯克

You can use numpy.memmap and let the operating system decide which parts of the image file to page in or out of RAM. 您可以使用numpy.memmap并让操作系统决定要在内存中调页或调出映像文件的哪些部分。 If you use 64-bit Python the virtual memory space is astronomic compared to the available RAM. 如果使用64位Python,则虚拟内存空间与可用RAM相比是天文数字。

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 在python中仅加载图像的一部分

Conversion from any file type to bitmap can be done in Python with this code: 可以使用以下代码在Python中完成从任何文件类型到位图的转换:

from PIL import Image
file_in = "inputCompressedImage.png"

img = Image.open(file_in)

file_out = "largeOutputFile.bmp"

img.save(file_out)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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