在python中将大图像文件作为数组读取
有谁知道如何使用python打开大图像文件?
我尝试使用ipython通过Windows命令提示符打开图像文件(大约2 GB),但每次将图像值更改为数组后都会崩溃。
Does anyone know how to open a large imagery file using python? I tried to open an imagery file (about 2 GB) through windows command prompt using ipython, but it crashes every time after I change image values into an array.
我的笔记本电脑是window7-64bit,配备4GB内存和Intel(R)Core(TM)i7-2860 QM CPU。
My laptop is window7-64bit with 4GB ram and Intel(R) Core(TM) i7-2860 QM CPU.
错误消息是:python.exe已停止工作问题导致程序无法正常工作。如果解决方案可用,Windows将关闭程序并通知您
The error message is: python.exe has stopped working A problem caused the program to stop working correctly. Windows will close the program and notify you if a solution is available
这是我的代码。
import Image
import numpy as num
im=Image.open('myimage.tif')
imarray=num.array(im)
你有多少RAM?你需要超过2GB的RAM来存储2-gig图像。我不知道 Image
在存储图像方面有多高效,但是字节列表对列表中的每个元素使用四个字节的空间,这样你就可以燃烧超过8GB的(虚拟)内存......而且需要很多耐心。 编辑:由于你只有4(或3)GB可以使用,这几乎肯定是你的问题。
How much RAM do you have? You'll need quite a bit more than 2GB of RAM to store a 2-gig image. I don't know how efficient Image
is at storing images, but a list of bytes uses four bytes of space for each element in the list, so you'll burn more than 8GB of (virtual) memory... and a lot of patience. Since you only have 4 (or 3) GB to play with, this is almost certainly your problem.
但你为什么要尝试将它转换为数字数组?使用 Image.open
返回的 im
对象的方法,如 PIL教程。
But why are you trying to convert it to a numeric array? Use the methods of the im
object returned by Image.open
, as in the PIL Tutorial.
我不知道您正在使用图像做什么,但也许您可以在不读取内存中的整个图像的情况下执行此操作,或者至少不将整个对象转换为 numpy
数组。如果可能的话,一点一点地读取它以避免炸毁你的机器:读取python生成器,并查看 Image.getdata()
方法,该方法返回一个像素值的图像一次。
I don't know what you're doing with the image, but perhaps you can do it without reading the entire image in memory, or at least without converting the entire object into a numpy
array. Read it bit by bit if possible to avoid blowing up your machine: Read up on python generators, and see the Image.getdata()
method, which returns your image one pixel value at a time.