mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-12 10:16:17 +03:00
added option to load truncated image-files
This commit is contained in:
parent
900f3a8ff7
commit
54d4f5eb3c
|
@ -35,6 +35,8 @@ MAXBLOCK = 65536
|
||||||
|
|
||||||
SAFEBLOCK = 1024*1024
|
SAFEBLOCK = 1024*1024
|
||||||
|
|
||||||
|
LOAD_TRUNCATED_IMAGES = False
|
||||||
|
|
||||||
ERRORS = {
|
ERRORS = {
|
||||||
-1: "image buffer overrun error",
|
-1: "image buffer overrun error",
|
||||||
-2: "decoding error",
|
-2: "decoding error",
|
||||||
|
@ -196,10 +198,22 @@ class ImageFile(Image.Image):
|
||||||
b = prefix
|
b = prefix
|
||||||
t = len(b)
|
t = len(b)
|
||||||
while True:
|
while True:
|
||||||
s = read(self.decodermaxblock)
|
try:
|
||||||
if not s:
|
s = read(self.decodermaxblock)
|
||||||
|
except IndexError as ie: # truncated png/gif
|
||||||
|
if LOAD_TRUNCATED_IMAGES:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
raise IndexError(ie)
|
||||||
|
|
||||||
|
if not s: # truncated jpeg
|
||||||
self.tile = []
|
self.tile = []
|
||||||
raise IOError("image file is truncated (%d bytes not processed)" % len(b))
|
|
||||||
|
if LOAD_TRUNCATED_IMAGES:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
raise IOError("image file is truncated (%d bytes not processed)" % len(b))
|
||||||
|
|
||||||
b = b + s
|
b = b + s
|
||||||
n, e = d.decode(b)
|
n, e = d.decode(b)
|
||||||
if n < 0:
|
if n < 0:
|
||||||
|
@ -212,7 +226,9 @@ class ImageFile(Image.Image):
|
||||||
|
|
||||||
self.fp = None # might be shared
|
self.fp = None # might be shared
|
||||||
|
|
||||||
if not self.map and e < 0:
|
if not LOAD_TRUNCATED_IMAGES and not self.map and e < 0:
|
||||||
|
# Note: If loading a truncated image results in an all black Image,
|
||||||
|
# the decoder wasn't able to decode anything.
|
||||||
raise_ioerror(e)
|
raise_ioerror(e)
|
||||||
|
|
||||||
# post processing
|
# post processing
|
||||||
|
|
Loading…
Reference in New Issue
Block a user