Python中的枕头不会让我打开图片(“超出限制") [英] Pillow in Python won't let me open image ("exceeds limit")

查看:106
本文介绍了Python中的枕头不会让我打开图片(“超出限制")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Python对某些天气数据进行模拟时遇到一些问题.数据以.tif格式提供,因此我使用以下代码尝试打开图像以将数据提取到numpy数组中.

Just having some problems running a simulation on some weather data in Python. The data was supplied in a .tif format, so I used the following code to try to open the image to extract the data into a numpy array.

from PIL import Image

im = Image.open('jan.tif')

但是当我运行这段代码时,出现以下错误:

But when I run this code I get the following error:

PIL.Image.DecompressionBombError: Image size (933120000 pixels) exceeds limit of 178956970 pixels, could be decompression bomb DOS attack.

看起来这只是针对这种攻击的某种防护,但是我实际上需要数据,并且该数据来自信誉良好的来源.有什么办法可以解决这个问题,还是我必须寻找另一种方法来解决这个问题?

It looks like this is just some kind of protection against this type of attack, but I actually need the data and it is from a reputable source. Is there any way to get around this or do I have to look for another way to do this?

推荐答案

尝试

PIL.Image.MAX_IMAGE_PIXELS = 933120000

如何发现这样的事情?

import PIL
print(PIL.__file__)  # prints, e. g., /usr/lib/python3/dist-packages/PIL/__init__.py

然后

cd /usr/lib/python3/dist-packages/PIL
grep -r -A 2 'exceeds limit' .

打印

./Image.py:            "Image size (%d pixels) exceeds limit of %d pixels, "
./Image.py-            "could be decompression bomb DOS attack." %
./Image.py-            (pixels, MAX_IMAGE_PIXELS),

然后

grep -r MAX_IMAGE_PIXELS .

打印

./Image.py:MAX_IMAGE_PIXELS = int(1024 * 1024 * 1024 / 4 / 3)
./Image.py:    if MAX_IMAGE_PIXELS is None:
./Image.py:    if pixels > MAX_IMAGE_PIXELS:
./Image.py:            (pixels, MAX_IMAGE_PIXELS),

然后

python3
import PIL.Image
PIL.Image.MAX_IMAGE_PIXELS = 933120000

可以正常工作,并且可以解决您的问题.

Works without complaint and fixes your issue.

这篇关于Python中的枕头不会让我打开图片(“超出限制")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆