Python 中的 Pillow 不允许我打开图像(“超出限制")

Pillow in Python won#39;t let me open image (quot;exceeds limitquot;)(Python 中的 Pillow 不允许我打开图像(“超出限制))

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

问题描述

只是在 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 中的 Pillow 不允许我打开图像(“超出限制")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Python 中的 Pillow 不允许我打开图像(“超出限制")

基础教程推荐