2017-03-08 248 views
2

我使用Pillow lib創建縮略圖。我創造了很多人,居然超過10000Python Pillow - ValueError:解壓縮數據太大

程序工作正常,但加工後的四圍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上執行此操作。

+1

我不太清楚是什麼導致了這個錯誤,但是你總是可以添加第二個錯誤處理位來至少記錄哪些文件導致這個錯誤,並繼續執行你的列表,例如:''',除了ValueError,e:print「不能創建'%s'的縮略圖「%infile 」錯誤:' %s'「%e''' –

+1

我不知道緩衝一下可能會阻止嗎?聽起來就像程序內存不足。你是否也確定它不會在同一點崩潰? – celestialroad

+0

@celestialroad有趣的是,它對於第一批1500張照片來說工作得很好。我現在將測試來自James Kent的日誌選項,以查看更多 – Marcel

回答

3

這是爲了防止運行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/

從最近遇到的問題:

If you set ImageFile.LOAD_TRUNCATED_IMAGES to true, it will suppress the error (but still not read the large metadata). Alternately, you can change set the values here: https://github.com/python-pillow/Pillow/ blob/master/PIL/PngImagePlugin.py#L74

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

+0

「ImageFile.LOAD_TRUNCATED_IMAGES爲true」。我試過了,它不起作用。 – notalentgeek

相關問題