mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-26 18:06:18 +03:00
add formats parameter to Image.open to restrict list of formats attempting to read an image file
This commit is contained in:
parent
608ccd0594
commit
2b061b68db
|
@ -86,6 +86,26 @@ class TestImage:
|
||||||
# with pytest.raises(MemoryError):
|
# with pytest.raises(MemoryError):
|
||||||
# Image.new("L", (1000000, 1000000))
|
# Image.new("L", (1000000, 1000000))
|
||||||
|
|
||||||
|
def test_open_formats(self):
|
||||||
|
PNGFILE = "Tests/images/hopper.png"
|
||||||
|
JPGFILE = "Tests/images/hopper.jpg"
|
||||||
|
|
||||||
|
with pytest.raises(TypeError):
|
||||||
|
Image.open(PNGFILE, formats=123)
|
||||||
|
|
||||||
|
for formats in [["JPEG"], ("JPEG",)]:
|
||||||
|
with pytest.raises(UnidentifiedImageError):
|
||||||
|
Image.open(PNGFILE, formats=formats)
|
||||||
|
|
||||||
|
with Image.open(JPGFILE, formats=formats) as im:
|
||||||
|
assert im.mode == "RGB"
|
||||||
|
assert im.size == (128, 128)
|
||||||
|
|
||||||
|
for file in [PNGFILE, JPGFILE]:
|
||||||
|
with Image.open(file, formats=None) as im:
|
||||||
|
assert im.mode == "RGB"
|
||||||
|
assert im.size == (128, 128)
|
||||||
|
|
||||||
def test_width_height(self):
|
def test_width_height(self):
|
||||||
im = Image.new("RGB", (1, 2))
|
im = Image.new("RGB", (1, 2))
|
||||||
assert im.width == 1
|
assert im.width == 1
|
||||||
|
|
|
@ -2836,7 +2836,7 @@ def _decompression_bomb_check(size):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def open(fp, mode="r"):
|
def open(fp, mode="r", formats=None):
|
||||||
"""
|
"""
|
||||||
Opens and identifies the given image file.
|
Opens and identifies the given image file.
|
||||||
|
|
||||||
|
@ -2867,6 +2867,11 @@ def open(fp, mode="r"):
|
||||||
"Binary data must be used instead."
|
"Binary data must be used instead."
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if formats is None:
|
||||||
|
formats = ID
|
||||||
|
elif not isinstance(formats, (list, tuple)):
|
||||||
|
raise TypeError("formats must be a list or tuple")
|
||||||
|
|
||||||
exclusive_fp = False
|
exclusive_fp = False
|
||||||
filename = ""
|
filename = ""
|
||||||
if isinstance(fp, Path):
|
if isinstance(fp, Path):
|
||||||
|
@ -2890,8 +2895,8 @@ def open(fp, mode="r"):
|
||||||
|
|
||||||
accept_warnings = []
|
accept_warnings = []
|
||||||
|
|
||||||
def _open_core(fp, filename, prefix):
|
def _open_core(fp, filename, prefix, formats):
|
||||||
for i in ID:
|
for i in formats:
|
||||||
try:
|
try:
|
||||||
factory, accept = OPEN[i]
|
factory, accept = OPEN[i]
|
||||||
result = not accept or accept(prefix)
|
result = not accept or accept(prefix)
|
||||||
|
@ -2913,11 +2918,11 @@ def open(fp, mode="r"):
|
||||||
raise
|
raise
|
||||||
return None
|
return None
|
||||||
|
|
||||||
im = _open_core(fp, filename, prefix)
|
im = _open_core(fp, filename, prefix, formats)
|
||||||
|
|
||||||
if im is None:
|
if im is None:
|
||||||
if init():
|
if init():
|
||||||
im = _open_core(fp, filename, prefix)
|
im = _open_core(fp, filename, prefix, formats)
|
||||||
|
|
||||||
if im:
|
if im:
|
||||||
im._exclusive_fp = exclusive_fp
|
im._exclusive_fp = exclusive_fp
|
||||||
|
|
Loading…
Reference in New Issue
Block a user