简体   繁体   中英

How to load images larger than MAX_IMAGE_PIXELS with PIL?

I'm trying to load some images into my Jupiter Notebook but PIL.Image.open() says that the image is too large. The MAX_IMAGE_PIXEL is set in the PIL Image source code but my image is much larger. I'm wondering if there's a way around this?

The code below works for smaller images. I've looked into trying to manually set the MAX_IMAGE_PIXEL but can't seem to do so.

import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

im = np.array(Image.open('data/big_image.jpg'), dtype=np.uint8)

# Create figure and axes
fig,ax = plt.subplots(1,figsize=(10,10))

# Display the image
ax.imshow(im)

plt.show()

The code above returns the following error:

---------------------------------------------------------------------------
DecompressionBombError                    Traceback (most recent call last)
<ipython-input-15-09854c1c6343> in <module>
      3 from PIL import Image
      4 
----> 5 im = np.array(Image.open('data/big_image.jpg'), dtype=np.uint8)
      6 
      7 # Create figure and axes

/opt/anaconda3/lib/python3.7/site-packages/PIL/Image.py in open(fp, mode)
   2640         return None
   2641 
-> 2642     im = _open_core(fp, filename, prefix)
   2643 
   2644     if im is None:

/opt/anaconda3/lib/python3.7/site-packages/PIL/Image.py in _open_core(fp, filename, prefix)
   2631                     fp.seek(0)
   2632                     im = factory(fp, filename)
-> 2633                     _decompression_bomb_check(im.size)
   2634                     return im
   2635             except (SyntaxError, IndexError, TypeError, struct.error):

/opt/anaconda3/lib/python3.7/site-packages/PIL/Image.py in _decompression_bomb_check(size)
   2566             "Image size (%d pixels) exceeds limit of %d pixels, "
   2567             "could be decompression bomb DOS attack." %
-> 2568             (pixels, 2 * MAX_IMAGE_PIXELS))
   2569 
   2570     if pixels > MAX_IMAGE_PIXELS:

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

Any help is appreciated!

The DecompressionBombError from Pillow is a safety feature for web services, but if you trust the source of the image then it's just an arbitrary limit. An RGB image with 1435500544 pixels has 24 bits per pixel, so it would require roughly 4.3 GB of RAM, which is far beyond the default Pillow limit of 178956970 pixels ( about 0.5GB for an RGB image .)
According to their docs, you can do something like:

import numpy as np
from PIL import Image
Image.MAX_IMAGE_PIXELS = None

im = np.array(Image.open('data/big_image.jpg'), dtype=np.uint8)

And it should work. If you anticipate working with large images, a good alternative to Pillow is OpenCV . It's very fast and offers a suite of algorithms oriented around computer vision. The getting started tutorial covers image loading.

import cv2

# Load an color image as a numpy array
img = cv2.imread('messi5.jpg',1)

It's important to note that OpenCV stacks channels as Blue-Green-Red and Pillow stacks them as Red-Green-Blue, but as long as you use the OpenCV API you'll have plenty of options to convert to RGB or any other color space you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM