[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
This commit is contained in:
pre-commit-ci[bot] 2023-07-07 12:31:37 +00:00
parent 823e3ddcf3
commit 05c3646058
2 changed files with 51 additions and 45 deletions

View File

@ -106,65 +106,65 @@ class TestImage:
assert p.pretty_output == "<PIL.Image.Image image mode=L size=100x100>" assert p.pretty_output == "<PIL.Image.Image image mode=L size=100x100>"
def test_repr_mimebundle(self): def test_repr_mimebundle(self):
im = Image.new('L', (100, 100)) im = Image.new("L", (100, 100))
# blank image should be most efficiently encoded as PNG # blank image should be most efficiently encoded as PNG
bundle = im._repr_mimebundle_() bundle = im._repr_mimebundle_()
with Image.open(io.BytesIO(bundle['image/png'])) as im2: with Image.open(io.BytesIO(bundle["image/png"])) as im2:
assert im2.format == 'PNG' assert im2.format == "PNG"
assert_image_equal(im, im2) assert_image_equal(im, im2)
# include pointless restriction # include pointless restriction
bundle = im._repr_mimebundle_(exclude=['test/plain']) bundle = im._repr_mimebundle_(exclude=["test/plain"])
with Image.open(io.BytesIO(bundle['image/png'])) as im2: with Image.open(io.BytesIO(bundle["image/png"])) as im2:
assert im2.format == 'PNG' assert im2.format == "PNG"
assert_image_equal(im, im2) assert_image_equal(im, im2)
bundle = im._repr_mimebundle_(include=['image/png']) bundle = im._repr_mimebundle_(include=["image/png"])
with Image.open(io.BytesIO(bundle['image/png'])) as im2: with Image.open(io.BytesIO(bundle["image/png"])) as im2:
assert im2.format == 'PNG' assert im2.format == "PNG"
assert_image_equal(im, im2) assert_image_equal(im, im2)
# force jpeg to be selected # force jpeg to be selected
bundle = im._repr_mimebundle_(include=['image/jpeg']) bundle = im._repr_mimebundle_(include=["image/jpeg"])
with Image.open(io.BytesIO(bundle['image/jpeg'])) as im2: with Image.open(io.BytesIO(bundle["image/jpeg"])) as im2:
assert im2.format == 'JPEG' assert im2.format == "JPEG"
assert_image_equal(im, im2, 17) assert_image_equal(im, im2, 17)
# force jpeg to be selected in a different way # force jpeg to be selected in a different way
bundle = im._repr_mimebundle_(exclude=['image/png']) bundle = im._repr_mimebundle_(exclude=["image/png"])
with Image.open(io.BytesIO(bundle['image/jpeg'])) as im2: with Image.open(io.BytesIO(bundle["image/jpeg"])) as im2:
assert im2.format == 'JPEG' assert im2.format == "JPEG"
assert_image_equal(im, im2, 17) assert_image_equal(im, im2, 17)
# make sure higher bit depths get converted down to 8BPC with warnings # make sure higher bit depths get converted down to 8BPC with warnings
high = Image.new('I;16', (100, 100)) high = Image.new("I;16", (100, 100))
with pytest.warns(UserWarning): with pytest.warns(UserWarning):
bundle = high._repr_mimebundle_() bundle = high._repr_mimebundle_()
with Image.open(io.BytesIO(bundle['image/png'])) as im2: with Image.open(io.BytesIO(bundle["image/png"])) as im2:
assert im2.format == 'PNG' assert im2.format == "PNG"
assert_image_equal(im, im2) assert_image_equal(im, im2)
high = Image.new('F', (100, 100)) high = Image.new("F", (100, 100))
with pytest.warns(UserWarning): with pytest.warns(UserWarning):
bundle = high._repr_mimebundle_() bundle = high._repr_mimebundle_()
with Image.open(io.BytesIO(bundle['image/png'])) as im2: with Image.open(io.BytesIO(bundle["image/png"])) as im2:
assert im2.format == 'PNG' assert im2.format == "PNG"
assert_image_equal(im, im2) assert_image_equal(im, im2)
high = Image.new('I', (100, 100)) high = Image.new("I", (100, 100))
with pytest.warns(UserWarning): with pytest.warns(UserWarning):
bundle = high._repr_mimebundle_() bundle = high._repr_mimebundle_()
with Image.open(io.BytesIO(bundle['image/png'])) as im2: with Image.open(io.BytesIO(bundle["image/png"])) as im2:
assert im2.format == 'PNG' assert im2.format == "PNG"
assert_image_equal(im, im2) assert_image_equal(im, im2)
# make sure large image gets scaled down with a warning # make sure large image gets scaled down with a warning
im = Image.new('L', [3000, 3000]) im = Image.new("L", [3000, 3000])
with pytest.warns(UserWarning): with pytest.warns(UserWarning):
bundle = im._repr_mimebundle_() bundle = im._repr_mimebundle_()
with Image.open(io.BytesIO(bundle['image/png'])) as im2: with Image.open(io.BytesIO(bundle["image/png"])) as im2:
assert im2.size == (1500, 1500) assert im2.size == (1500, 1500)
assert_image_equal(im.resize(im2.size), im2) assert_image_equal(im.resize(im2.size), im2)

View File

@ -462,38 +462,43 @@ def _getscaleoffset(expr):
_IPYTHON_MODE_MAP = { _IPYTHON_MODE_MAP = {
'La': 'LA', "La": "LA",
'LAB': 'RGB', "LAB": "RGB",
'HSV': 'RGB', "HSV": "RGB",
'RGBX': 'RGB', "RGBX": "RGB",
'RGBa': 'RGBA', "RGBa": "RGBA",
} }
_VALID_MODES_FOR_FORMAT = { _VALID_MODES_FOR_FORMAT = {
'JPEG': {'L', 'RGB', 'YCbCr', 'CMYK'}, "JPEG": {"L", "RGB", "YCbCr", "CMYK"},
'PNG': {'1', 'P', 'L', 'RGB', 'RGBA', 'LA', 'PA'}, "PNG": {"1", "P", "L", "RGB", "RGBA", "LA", "PA"},
} }
def _to_ipython_image(image): def _to_ipython_image(image):
"""Simplify image to something suitable for display in IPython/Jupyter. """Simplify image to something suitable for display in IPython/Jupyter.
Notably, convert to 8BPC and rescale by some integer factor. Notably, convert to 8BPC and rescale by some integer factor.
""" """
if image.mode in {'I', 'F'}: if image.mode in {"I", "F"}:
warnings.warn("image mode doesn't have well defined min/max value, using extrema") warnings.warn(
"image mode doesn't have well defined min/max value, using extrema"
)
# linearly transform extrema to fit in [0, 255] # linearly transform extrema to fit in [0, 255]
# this should have a similar result as Image.histogram # this should have a similar result as Image.histogram
lo, hi = image.getextrema() lo, hi = image.getextrema()
scale = 256 / (hi - lo) if lo != hi else 1 scale = 256 / (hi - lo) if lo != hi else 1
image = image.point(lambda e: (e - lo) * scale).convert('L') image = image.point(lambda e: (e - lo) * scale).convert("L")
elif image.mode == 'I;16': elif image.mode == "I;16":
warnings.warn("converting 16BPC image to 8BPC for display in IPython/Jupyter") warnings.warn("converting 16BPC image to 8BPC for display in IPython/Jupyter")
# linearly transform max down to 255 # linearly transform max down to 255
image = image.point(lambda e: e / 256).convert('L') image = image.point(lambda e: e / 256).convert("L")
# shrink large images so they don't take too long to transfer/render # shrink large images so they don't take too long to transfer/render
factor = max(image.size) // IPYTHON_RESIZE_THRESHOLD factor = max(image.size) // IPYTHON_RESIZE_THRESHOLD
if factor > 1: if factor > 1:
warnings.warn("scaling large image down to improve performance in IPython/Jupyter") warnings.warn(
"scaling large image down to improve performance in IPython/Jupyter"
)
image = image.reduce(factor) image = image.reduce(factor)
# process remaining modes into things supported by writers # process remaining modes into things supported by writers
@ -502,6 +507,7 @@ def _to_ipython_image(image):
return image return image
def _encode_ipython_image(image, image_format): def _encode_ipython_image(image, image_format):
"""Encode specfied image into something IPython/Jupyter supports. """Encode specfied image into something IPython/Jupyter supports.
@ -706,8 +712,8 @@ class Image:
return None return None
return _encode_ipython_image(image, image_format) return _encode_ipython_image(image, image_format)
jpeg = encode('image/jpeg', 'JPEG') jpeg = encode("image/jpeg", "JPEG")
png = encode('image/png', 'PNG') png = encode("image/png", "PNG")
# prefer lossless format if it's not significantly larger # prefer lossless format if it's not significantly larger
if jpeg and png: if jpeg and png:
@ -718,8 +724,8 @@ class Image:
png = None png = None
return { return {
'image/jpeg': jpeg, "image/jpeg": jpeg,
'image/png': png, "image/png": png,
} }
def _repr_png_(self): def _repr_png_(self):
@ -727,14 +733,14 @@ class Image:
:returns: PNG version of the image as bytes :returns: PNG version of the image as bytes
""" """
return _encode_ipython_image(_to_ipython_image(self), 'PNG') return _encode_ipython_image(_to_ipython_image(self), "PNG")
def _repr_jpeg_(self): def _repr_jpeg_(self):
"""iPython display hook support for JPEG format. """iPython display hook support for JPEG format.
:returns: JPEG version of the image as bytes :returns: JPEG version of the image as bytes
""" """
return _encode_ipython_image(_to_ipython_image(self), 'JPEG') return _encode_ipython_image(_to_ipython_image(self), "JPEG")
@property @property
def __array_interface__(self): def __array_interface__(self):