mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-08-22 21:24:46 +03:00
Added formats parameter to init
This commit is contained in:
parent
608ccd0594
commit
ccb75705bd
|
@ -86,6 +86,29 @@ class TestImage:
|
||||||
# with pytest.raises(MemoryError):
|
# with pytest.raises(MemoryError):
|
||||||
# Image.new("L", (1000000, 1000000))
|
# Image.new("L", (1000000, 1000000))
|
||||||
|
|
||||||
|
def test_init(self):
|
||||||
|
try:
|
||||||
|
# No formats
|
||||||
|
Image.init([])
|
||||||
|
assert len(Image.ID) == 0
|
||||||
|
for ext in (".jpg", ".png"):
|
||||||
|
with pytest.raises(UnidentifiedImageError):
|
||||||
|
Image.open("Tests/images/hopper" + ext)
|
||||||
|
|
||||||
|
# Only JPEG
|
||||||
|
Image.init(["JPEG"])
|
||||||
|
assert len(Image.ID) != 0
|
||||||
|
Image.open("Tests/images/hopper.jpg")
|
||||||
|
with pytest.raises(UnidentifiedImageError):
|
||||||
|
Image.open("Tests/images/hopper.png")
|
||||||
|
|
||||||
|
finally:
|
||||||
|
# All formats
|
||||||
|
Image.init(True)
|
||||||
|
assert len(Image.ID) != 0
|
||||||
|
for ext in (".jpg", ".png"):
|
||||||
|
Image.open("Tests/images/hopper" + ext)
|
||||||
|
|
||||||
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
|
||||||
|
|
|
@ -204,6 +204,7 @@ if hasattr(core, "DEFAULT_STRATEGY"):
|
||||||
# Registries
|
# Registries
|
||||||
|
|
||||||
ID = []
|
ID = []
|
||||||
|
EVERY_ID = []
|
||||||
OPEN = {}
|
OPEN = {}
|
||||||
MIME = {}
|
MIME = {}
|
||||||
SAVE = {}
|
SAVE = {}
|
||||||
|
@ -390,14 +391,24 @@ def preinit():
|
||||||
_initialized = 1
|
_initialized = 1
|
||||||
|
|
||||||
|
|
||||||
def init():
|
def init(formats=None):
|
||||||
"""
|
"""
|
||||||
Explicitly initializes the Python Imaging Library. This function
|
Explicitly initializes the Python Imaging Library. This function
|
||||||
loads all available file format drivers.
|
loads all available file format drivers.
|
||||||
|
|
||||||
|
:param formats: Which formats to use when identifying images. The default value of
|
||||||
|
``None`` will load all formats at first, or remember the previous
|
||||||
|
setting if called repeatedly. If you have previously provided a
|
||||||
|
list of formats to use, ``True`` must be used to remove the
|
||||||
|
restriction and use all formats.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
global _initialized
|
global _initialized, ID, EVERY_ID
|
||||||
if _initialized >= 2:
|
if _initialized >= 2:
|
||||||
|
if formats is True:
|
||||||
|
ID = EVERY_ID
|
||||||
|
elif formats is not None:
|
||||||
|
ID = formats
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
for plugin in _plugins:
|
for plugin in _plugins:
|
||||||
|
@ -406,8 +417,11 @@ def init():
|
||||||
__import__("PIL.%s" % plugin, globals(), locals(), [])
|
__import__("PIL.%s" % plugin, globals(), locals(), [])
|
||||||
except ImportError as e:
|
except ImportError as e:
|
||||||
logger.debug("Image: failed to import %s: %s", plugin, e)
|
logger.debug("Image: failed to import %s: %s", plugin, e)
|
||||||
|
EVERY_ID = ID
|
||||||
|
if formats is not None and formats is not True:
|
||||||
|
ID = formats
|
||||||
|
|
||||||
if OPEN or SAVE:
|
if OPEN or SAVE or (not formats and formats is not None):
|
||||||
_initialized = 2
|
_initialized = 2
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user