diff --git a/Tests/test_file_mic.py b/Tests/test_file_mic.py index 9a6f13ea3..8140342c0 100644 --- a/Tests/test_file_mic.py +++ b/Tests/test_file_mic.py @@ -41,6 +41,8 @@ def test_is_animated() -> None: def test_tell() -> None: with Image.open(TEST_FILE) as im: assert im.tell() == 0 + im.seek(1) + assert im.tell() == 1 def test_seek() -> None: diff --git a/Tests/test_image_array.py b/Tests/test_image_array.py index d7e6c562c..db4e56f5a 100644 --- a/Tests/test_image_array.py +++ b/Tests/test_image_array.py @@ -95,6 +95,10 @@ def test_fromarray_strides_without_tobytes() -> None: class Wrapper: def __init__(self, arr_params: dict[str, Any]) -> None: self.__array_interface__ = arr_params + self.data = numpy.zeros(arr_params['shape'], dtype=numpy.uint8).tobytes() + + def tostring(self): + return self.data with pytest.raises(ValueError): wrapped = Wrapper({"shape": (1, 1), "strides": (1, 1)}) diff --git a/Tests/test_imageshow.py b/Tests/test_imageshow.py index 4e9291fbb..523d695ad 100644 --- a/Tests/test_imageshow.py +++ b/Tests/test_imageshow.py @@ -94,3 +94,31 @@ def test_ipythonviewer() -> None: im = hopper() assert test_viewer.show(im) == 1 + +def test_viewer_exceptions() -> None: + viewer = ImageShow.Viewer() + + with pytest.raises(NotImplementedError): + viewer.show_image(Image.new("L", (1, 1))) + + with pytest.raises(NotImplementedError): + viewer.save_image(Image.new("L", (1, 1))) + +def test_viewer_show_edge_cases() -> None: + class TestViewer(ImageShow.Viewer): + def show_image(self, image: Image.Image, **options: Any) -> bool: + self.methodCalled = True + return True + + viewer = TestViewer() + ImageShow.register(viewer) + + for mode in ["", "special characters", "a" * 1000]: + viewer.methodCalled = False + with hopper(mode) as im: + assert ImageShow.show(im) + assert viewer.methodCalled + + # Restore original state + ImageShow._viewers.pop(0) + \ No newline at end of file diff --git a/Tests/test_uploader.py b/Tests/test_uploader.py index d55ceb4be..e6427592f 100644 --- a/Tests/test_uploader.py +++ b/Tests/test_uploader.py @@ -1,15 +1,46 @@ from __future__ import annotations +import pytest +from PIL import Image + from .helper import assert_image_equal, assert_image_similar, hopper +@pytest.fixture +def result(): + return hopper("P").convert("RGB") -def check_upload_equal() -> None: - result = hopper("P").convert("RGB") - target = hopper("RGB") +@pytest.fixture +def target(): + return hopper("RGB") + +def test_upload_equal(result, target): assert_image_equal(result, target) +def test_upload_similar(result, target, epsilon): + assert_image_similar(result, target, epsilon) -def check_upload_similar() -> None: +@pytest.mark.parametrize("epsilon", [0, 1, 100]) +def test_upload_similar_parametrized(result, target, epsilon): + assert_image_similar(result, target, epsilon) + +def test_upload_equal_different_formats(): + result = Image.open("Tests/images/hopper_webp.png").convert("RGB") + target = Image.open("Tests/images/hopper_webp.tif").convert("RGB") + assert_image_equal(result, target) + +def test_upload_equal_different_sizes(): + result = hopper("P").resize((100, 100)).convert("RGB") + target = hopper("RGB").resize((200, 200)) + assert_image_equal(result, target) + +def test_upload_not_similar(): result = hopper("P").convert("RGB") - target = hopper("RGB") - assert_image_similar(result, target, 0) + target = Image.new("RGB", (100, 100), "white") + with pytest.raises(AssertionError): + assert_image_similar(result, target, 0) + +def test_upload_equal_different_modes(): + result = hopper("L").convert("RGB") + target = hopper("1").convert("RGB") + assert_image_equal(result, target) + \ No newline at end of file diff --git a/boy meets evil.png b/boy meets evil.png new file mode 100644 index 000000000..046a98a20 Binary files /dev/null and b/boy meets evil.png differ diff --git a/display_image.py b/display_image.py new file mode 100644 index 000000000..1c2543bb4 --- /dev/null +++ b/display_image.py @@ -0,0 +1,15 @@ +import time +import matplotlib +matplotlib.use('TkAgg') +from PIL import Image +import matplotlib.pyplot as plt + +# Open an image file +img = Image.open('boy meets evil.png') + +# Display the image +plt.imshow(img) +plt.show() + +# Keep the script running for a while +time.sleep(10) # keeps the script running for 10 seconds \ No newline at end of file