update tests to include desired mimebundle behaviour

This commit is contained in:
Sam Mason 2023-07-07 13:12:19 +01:00
parent f089c2db8c
commit c194d56ba5
No known key found for this signature in database
GPG Key ID: 0D059A3A7ECA31DA
3 changed files with 67 additions and 8 deletions

View File

@ -930,10 +930,8 @@ class TestFileJpeg:
assert_image_similar(im, repr_jpeg, 17)
def test_repr_jpeg_error(self):
im = hopper("F")
with pytest.raises(ValueError):
im._repr_jpeg_()
im = hopper("BGR;24")
assert im._repr_jpeg_() is None
@pytest.mark.skipif(not is_win32(), reason="Windows only")

View File

@ -533,10 +533,8 @@ class TestFilePng:
assert_image_equal(im, repr_png)
def test_repr_png_error(self):
im = hopper("F")
with pytest.raises(ValueError):
im._repr_png_()
im = hopper("BGR;24")
assert im._repr_png_() is None
def test_chunk_order(self, tmp_path):
with Image.open("Tests/images/icc_profile.png") as im:

View File

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