82

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?

3 Answers 3

111

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.

1
  • 48
    If you 100% trust your inputs, you can also disable the check completely with PIL.Image.MAX_IMAGE_PIXELS = None
    – OrangeDog
    Oct 8, 2018 at 10:08
70

After the imports, add :

Image.MAX_IMAGE_PIXELS = None
2
  • 2
    Dude, you are great
    – ASLAN
    Feb 3, 2021 at 14:43
  • 1
    sometimes comparisons doing this would fail the conditions such as if w*h > Image.MAX_IMAGE_PIXELS: with an error TypeError: '>' not supported between instances of 'int' and 'NoneType' Jan 23 at 10:41
1

PIL.Image.MAX_IMAGE_PIXELS = None , instead of this use Image.MAX_IMAGE_PIXELS = None this is working fine

1
  • 1
    This answer is already there.
    – Azhar Khan
    Nov 15, 2022 at 13:24

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.