python-pillow / Pillow

Python Imaging Library (Fork)
https://python-pillow.org
Other
12.16k stars 2.21k forks source link

Image.DecompressionBombError: Image size (1009731888 pixels) exceeds limit. Expand the number of pixels #5218

Closed nadzhou closed 3 years ago

nadzhou commented 3 years ago

Expand the number of MBs for the image input. I deal with pictures that are more than 90 MB per picture. Here's the total error that I get:

    Traceback (most recent call last):
  File "image2image.py", line 106, in <module>
    image = Image.open("../" + img_paths_for_pillow[i])
  File "/home/nadzhou/anaconda3/envs/gun_detection/lib/python3.6/site-packages/PIL/Image.py", line 2948, in open
    im = _open_core(fp, filename, prefix, formats)
  File "/home/nadzhou/anaconda3/envs/gun_detection/lib/python3.6/site-packages/PIL/Image.py", line 2931, in _open_core
    _decompression_bomb_check(im.size)
  File "/home/nadzhou/anaconda3/envs/gun_detection/lib/python3.6/site-packages/PIL/Image.py", line 2842, in _decompression_bomb_check
    f"Image size ({pixels} pixels) exceeds limit of {2 * MAX_IMAGE_PIXELS} "
**PIL.Image.DecompressionBombError: Image size (1009731888 pixels) exceeds limit of 178956970 pixels, could be decompression bomb DOS attack.**
radarhere commented 3 years ago

You can change this behaviour from outside of Pillow like so

from PIL import Image
Image.MAX_IMAGE_PIXELS = 2000000000
radarhere commented 3 years ago

This functionality is documented at https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.open -

To protect against potential DOS attacks caused by “decompression bombs” (i.e. malicious files which decompress into a huge amount of data and are designed to crash or cause disruption by using up a lot of memory), Pillow will issue a DecompressionBombWarning if the number of pixels in an image is over a certain limit, PIL.Image.MAX_IMAGE_PIXELS.

This threshold can be changed by setting PIL.Image.MAX_IMAGE_PIXELS. It can be disabled by setting Image.MAX_IMAGE_PIXELS = None.

If desired, the warning can be turned into an error with warnings.simplefilter('error', Image.DecompressionBombWarning) or suppressed entirely with warnings.simplefilter('ignore', Image.DecompressionBombWarning). See also the logging documentation to have warnings output to the logging facility instead of stderr.

If the number of pixels is greater than twice PIL.Image.MAX_IMAGE_PIXELS, then a DecompressionBombError will be raised instead.

nadzhou commented 3 years ago

oh ok @radarhere you can close the issue.

i was actually using another repo and came across this problem.