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 不允许我打开图像(“超出限制")
![](/xwassets/images/pre.png)
![](/xwassets/images/next.png)
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01