Python中的枕头不会让我打开图像(“超出限制”)

问题描述 投票:7回答:1

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

from PIL import Image

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

但是当我运行此代码时,我收到以下错误:

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

看起来这只是对这种类型的攻击的某种保护,但我实际上需要数据,它来自一个有信誉的来源。有没有办法解决这个问题,还是我必须寻找另一种方法来做到这一点?

python image dataset
1个回答
15
投票

尝试

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

无怨无悔地工作并解决您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.