This commit is contained in:
Andrew Murray 2018-08-28 10:28:26 +00:00 committed by GitHub
commit c53ec4904b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 9 deletions

View File

@ -2557,7 +2557,7 @@ def _decompression_bomb_check(size):
DecompressionBombWarning) DecompressionBombWarning)
def open(fp, mode="r"): def open(fp, mode="r", format_args=None):
""" """
Opens and identifies the given image file. Opens and identifies the given image file.
@ -2601,13 +2601,13 @@ def open(fp, mode="r"):
preinit() preinit()
def _open_core(fp, filename, prefix): def _open_core(fp, filename, prefix, format_args):
for i in ID: for i in ID:
try: try:
factory, accept = OPEN[i] factory, accept = OPEN[i]
if not accept or accept(prefix): if not accept or accept(prefix):
fp.seek(0) fp.seek(0)
im = factory(fp, filename) im = factory(fp, filename, format_args)
_decompression_bomb_check(im.size) _decompression_bomb_check(im.size)
return im return im
except (SyntaxError, IndexError, TypeError, struct.error): except (SyntaxError, IndexError, TypeError, struct.error):
@ -2617,11 +2617,11 @@ def open(fp, mode="r"):
continue continue
return None return None
im = _open_core(fp, filename, prefix) im = _open_core(fp, filename, prefix, format_args)
if im is None: if im is None:
if init(): if init():
im = _open_core(fp, filename, prefix) im = _open_core(fp, filename, prefix, format_args)
if im: if im:
im._exclusive_fp = exclusive_fp im._exclusive_fp = exclusive_fp

View File

@ -75,7 +75,7 @@ def _tilesort(t):
class ImageFile(Image.Image): class ImageFile(Image.Image):
"Base class for image file format handlers." "Base class for image file format handlers."
def __init__(self, fp=None, filename=None): def __init__(self, fp=None, filename=None, format_args=None):
Image.Image.__init__(self) Image.Image.__init__(self)
self._min_frame = 0 self._min_frame = 0
@ -86,6 +86,10 @@ class ImageFile(Image.Image):
self.decoderconfig = () self.decoderconfig = ()
self.decodermaxblock = MAXBLOCK self.decodermaxblock = MAXBLOCK
if format_args is None:
format_args = {}
self.formats_args = format_args
if isPath(fp): if isPath(fp):
# filename # filename
self.fp = open(fp, "rb") self.fp = open(fp, "rb")

View File

@ -776,14 +776,14 @@ def _save_cjpeg(im, fp, filename):
## ##
# Factory for making JPEG and MPO instances # Factory for making JPEG and MPO instances
def jpeg_factory(fp=None, filename=None): def jpeg_factory(fp=None, filename=None, format_args=None):
im = JpegImageFile(fp, filename) im = JpegImageFile(fp, filename, format_args)
try: try:
mpheader = im._getmp() mpheader = im._getmp()
if mpheader[45057] > 1: if mpheader[45057] > 1:
# It's actually an MPO # It's actually an MPO
from .MpoImagePlugin import MpoImageFile from .MpoImagePlugin import MpoImageFile
im = MpoImageFile(fp, filename) im = MpoImageFile(fp, filename, format_args)
except (TypeError, IndexError): except (TypeError, IndexError):
# It is really a JPEG # It is really a JPEG
pass pass