我正在尝试在python 3.4上使用pillow 3.3.1来存储大图像。这些图像往往在1到4 GB的范围内,如uint8 RGB像素。Linux和OSX给了我相同的结果。
from PIL import Image
import numpy as np
imgArray = np.random.randint(255, size=(39000, 35000, 3)).astype(np.uint8)
print("buffer size:", imgArray.size)
print("image max bytes:", 2**32)
pilImage = Image.fromarray(imgArray)我得到了以下输出
buffer size: 4095000000
image max bytes: 4294967296
Traceback (most recent call last):
File "storeLargeImage.py", line 6, in <module>
pilImage = Image.fromarray(imgArray)
File "/home/mpesavento/miniconda3/lib/python3.4/site-packages/PIL/Image.py", line 2189, in fromarray
return frombuffer(mode, size, obj, "raw", rawmode, 0, 1)
File "/home/mpesavento/miniconda3/lib/python3.4/site-packages/PIL/Image.py", line 2139, in frombuffer
return frombytes(mode, size, data, decoder_name, args)
File "/home/mpesavento/miniconda3/lib/python3.4/site-packages/PIL/Image.py", line 2074, in frombytes
im.frombytes(data, decoder_name, args)
File "/home/mpesavento/miniconda3/lib/python3.4/site-packages/PIL/Image.py", line 736, in frombytes
s = d.decode(data)
OverflowError: size does not fit in an int缓冲区比我认为PIL在Python3中使用的最大值要小,我认为Python3使用了uint32作为缓冲区长度。Python2中的PIL使用int32,最大值为2**31-1。
在我们确定要存储的编解码器之前,这个bug就出现了。例如,我想存储无损的png或tif,通过
pilImage.save(BytesIO(), format="png")
或
pilImage.save(BytesIO(), format="tiff")
如何保存大于2 GB (2147483647字节)的图像?
编辑:它看起来应该是fixed a while ago。不确定为什么问题仍然出现。
发布于 2016-10-07 18:59:22
我知道你问过皮尔,但你可以试试libvips。它专门处理大图像(图像比可用RAM大),对于4 4gb的文件应该没有问题。这里有一些speed and memory use benchmarks on the vips website。
例如,我就是这么做的:
$ python
Python 2.7.12 (default, Jul 1 2016, 15:12:24)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyvips
>>> x = pyvips.Image.perlin(100000, 100000, uchar=True)
>>> x.write_to_file("x.tif", bigtiff=True)
>>>
$ ls -l x.tif
-rw-rw-r-- 1 john john 10000012844 Oct 7 11:43 x.tif生成10 an、8位、100,000 x 100,000像素的Perlin噪波图像。当然,这需要一点时间-在我的笔记本电脑上,最后一次写入大约需要三分钟,需要100MB的RAM。Python绑定记录为here。你可以使用move images between numpy and vips。
https://stackoverflow.com/questions/39907275
复制相似问题