首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >加载PIL.Image.fromArray时的PIL OverflowError

加载PIL.Image.fromArray时的PIL OverflowError
EN

Stack Overflow用户
提问于 2016-10-07 07:43:54
回答 1查看 970关注 0票数 0

我正在尝试在python 3.4上使用pillow 3.3.1来存储大图像。这些图像往往在1到4 GB的范围内,如uint8 RGB像素。Linux和OSX给了我相同的结果。

代码语言:javascript
复制
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)

我得到了以下输出

代码语言:javascript
复制
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。不确定为什么问题仍然出现。

EN

回答 1

Stack Overflow用户

发布于 2016-10-07 18:59:22

我知道你问过皮尔,但你可以试试libvips。它专门处理大图像(图像比可用RAM大),对于4 4gb的文件应该没有问题。这里有一些speed and memory use benchmarks on the vips website

例如,我就是这么做的:

代码语言:javascript
复制
$ 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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39907275

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档