简体   繁体   English

如何使用 PIL 加载大于 MAX_IMAGE_PIXELS 的图像?

[英]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.我正在尝试将一些图像加载到我的 Jupiter Notebook 中,但 PIL.Image.open() 说图像太大。 The MAX_IMAGE_PIXEL is set in the PIL Image source code but my image is much larger. MAX_IMAGE_PIXEL 在 PIL Image 源代码中设置,但我的图像要大得多。 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.我已经研究过尝试手动设置 MAX_IMAGE_PIXEL 但似乎无法这样做。

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. Pillow 的DecompressionBombError是 Web 服务的安全功能,但如果您信任图像的来源,那么它只是一个任意限制。 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 .)具有 1435500544 像素的 RGB 图像每像素有 24 位,因此它需要大约 4.3 GB 的 RAM,这远远超出了默认的 Pillow 限制178956970 像素( RGB 图像约为 0.5GB )。
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 .如果您打算使用大图像,则 Pillow 的一个很好的替代品是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.需要注意的是,OpenCV 将通道堆叠为 Blue-Green-Red,Pillow 将它们堆叠为 Red-Green-Blue,但是只要您使用OpenCV API,您将有很多选项可以转换为 RGB 或任何其他颜色空间你要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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