Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pillow in Python won't let me open image ("exceeds limit")

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?

like image 432
Tom Heeley Avatar asked Jul 03 '18 10:07

Tom Heeley


People also ask

How do I save an image as a Pillow in Python?

Saving an image can be achieved by using . save() method with PIL library's Image module in Python.

What does the Pillow module do in Python?

The Pillow library contains all the basic image processing functionality. You can do image resizing, rotation and transformation. Pillow module allows you to pull some statistics data out of image using histogram method, which later can be used for statistical analysis and automatic contrast enhancement.

Can Python open images?

Image. open() Opens and identifies the given image file. This is a lazy operation; this function identifies the file, but the file remains open and the actual image data is not read from the file until you try to process the data (or call the load() method).


2 Answers

Try

PIL.Image.MAX_IMAGE_PIXELS = 933120000

How to find out such a thing?

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

Then

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

prints

./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),

Then

grep -r MAX_IMAGE_PIXELS .

prints

./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),

Then

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

Works without complaint and fixes your issue.

like image 156
Alfe Avatar answered Oct 07 '22 16:10

Alfe


After the imports, add :

Image.MAX_IMAGE_PIXELS = None
like image 40
Jaire Marques Avatar answered Oct 07 '22 16:10

Jaire Marques