Python Pillow - ValueError:解压缩数据太大

时间:2017-03-08 12:20:28

标签: python pillow

我使用Pillow lib来创建缩略图。我必须创建很多,实际上超过10.000

该程序运行正常,但在处理大约1.500后,我收到以下错误:

    Traceback (most recent call last):
  File "thumb.py", line 15, in <module>
    im = Image.open('/Users/Marcel/images/07032017/' + infile)
  File "/Users/Marcel/product-/PIL/Image.py", line 2339, in open
    im = _open_core(fp, filename, prefix)
  File "/Users/Marcel/product-/PIL/Image.py", line 2329, in _open_core
    im = factory(fp, filename)
  File "/Users/Marcel/product-/PIL/ImageFile.py", line 97, in __init__
    self._open()
  File "/Users/Marcel/product-/PIL/PngImagePlugin.py", line 538, in _open
    s = self.png.call(cid, pos, length)
  File "/Users/Marcel/product-/PIL/PngImagePlugin.py", line 136, in call
    return getattr(self, "chunk_" + cid.decode('ascii'))(pos, length)
  File "/Users/Marcel/product-/PIL/PngImagePlugin.py", line 319, in chunk_iCCP
    icc_profile = _safe_zlib_decompress(s[i+2:])
  File "/Users/Marcel/product-/PIL/PngImagePlugin.py", line 90, in _safe_zlib_decompress
    raise ValueError("Decompressed Data Too Large")
ValueError: Decompressed Data Too Large

我的计划很直接:

import os, sys
import PIL
from PIL import Image

size = 235, 210
reviewedProductsList = open('products.txt', 'r')
reviewedProducts = reviewedProductsList.readlines()
t = map(lambda s: s.strip(), reviewedProducts)

print "Thumbs to create: '%s'" % len(reviewedProducts)

for infile in t:
    outfile = infile
    try:
        im = Image.open('/Users/Marcel/images/07032017/' + infile)
        im.thumbnail(size, Image.ANTIALIAS)
        print "thumb created"
        im.save('/Users/Marcel/product-/thumbs/' + outfile, "JPEG")
    except IOError, e:
        print "cannot create thumbnail for '%s'" % infile
        print "error: '%s'" % e

我在MacBook Pro上本地执行此操作。

2 个答案:

答案 0 :(得分:5)

这是为了防止因减压炸弹对运行Pillow的服务器造成潜在的DoS攻击。当发现解压缩的图像具有太大的元数据时,会发生这种情况。见http://pillow.readthedocs.io/en/4.0.x/handbook/image-file-formats.html?highlight=decompression#png

以下是CVE报告:https:// www.cvedetails.com/cve/CVE-2014-9601 /

最近一期:

  

如果将ImageFile.LOAD_TRUNCATED_IMAGES设置为true,则会禁止显示   错误(但仍未读取大型元数据)。或者,你可以   更改在此设置值:https://github.com/python-pillow/Pillow/ blob / master / PIL / PngImagePlugin.py#L74

https://github.com/python-pillow/Pillow/issues/2445

答案 1 :(得分:1)

以下代码将帮助您设置可接受的答案。

from PIL import PngImagePlugin
LARGE_ENOUGH_NUMBER = 100
PngImagePlugin.MAX_TEXT_CHUNK = LARGE_ENOUGH_NUMBER * (1024**2)

尚未记录如何设置此值。我希望人们觉得这有用。

相关问题