Pillow in Python won#39;t let me open image (quot;exceeds limitquot;)(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 不允许我打开图像(“超出限制")
基础教程推荐
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 筛选NumPy数组 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01