From c194d56ba55d370735a27281dd9b1f5e1ce88b43 Mon Sep 17 00:00:00 2001 From: Sam Mason Date: Fri, 7 Jul 2023 13:12:19 +0100 Subject: [PATCH] update tests to include desired mimebundle behaviour --- Tests/test_file_jpeg.py | 6 ++-- Tests/test_file_png.py | 6 ++-- Tests/test_image.py | 63 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 8 deletions(-) diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 0247527f5..d7dd0d298 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -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") diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py index c4db97905..4f456f79b 100644 --- a/Tests/test_file_png.py +++ b/Tests/test_file_png.py @@ -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: diff --git a/Tests/test_image.py b/Tests/test_image.py index 85f9f7d02..6e8d323bc 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -105,6 +105,69 @@ class TestImage: im._repr_pretty_(p, None) assert p.pretty_output == "" + 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"