Skip to content

Commit 23b7719

Browse files
committed
Merge remote-tracking branch 'upstream/master' into docs-imageshow
2 parents c40b0e5 + 713dd17 commit 23b7719

24 files changed

Lines changed: 321 additions & 135 deletions

.github/workflows/test-windows.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ jobs:
5252
python-version: ${{ matrix.python-version }}
5353
architecture: ${{ matrix.architecture }}
5454

55+
- name: Set up TCL
56+
if: "contains(matrix.python-version, 'pypy')"
57+
run: Write-Host "::set-env name=TCL_LIBRARY::$env:pythonLocation\tcl\tcl8.5"
58+
shell: pwsh
59+
5560
- name: Print build system information
5661
run: python .github/workflows/system-info.py
5762

Tests/images/variation_adobe.png

3 Bytes
Loading
-9 Bytes
Loading
7 Bytes
Loading
1.44 KB
Loading
1.41 KB
Loading
1.42 KB
Loading

Tests/test_file_webp.py

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import io
2+
13
import pytest
24
from PIL import Image, WebPImagePlugin
35

@@ -54,72 +56,65 @@ def test_read_rgb(self):
5456
# dwebp -ppm ../../Tests/images/hopper.webp -o hopper_webp_bits.ppm
5557
assert_image_similar_tofile(image, "Tests/images/hopper_webp_bits.ppm", 1.0)
5658

57-
def test_write_rgb(self, tmp_path):
58-
"""
59-
Can we write a RGB mode file to webp without error.
60-
Does it have the bits we expect?
61-
"""
62-
59+
def _roundtrip(self, tmp_path, mode, epsilon, args={}):
6360
temp_file = str(tmp_path / "temp.webp")
6461

65-
hopper(self.rgb_mode).save(temp_file)
62+
hopper(mode).save(temp_file, **args)
6663
with Image.open(temp_file) as image:
6764
assert image.mode == self.rgb_mode
6865
assert image.size == (128, 128)
6966
assert image.format == "WEBP"
7067
image.load()
7168
image.getdata()
7269

73-
# generated with: dwebp -ppm temp.webp -o hopper_webp_write.ppm
74-
assert_image_similar_tofile(
75-
image, "Tests/images/hopper_webp_write.ppm", 12.0
76-
)
70+
if mode == self.rgb_mode:
71+
# generated with: dwebp -ppm temp.webp -o hopper_webp_write.ppm
72+
assert_image_similar_tofile(
73+
image, "Tests/images/hopper_webp_write.ppm", 12.0
74+
)
7775

7876
# This test asserts that the images are similar. If the average pixel
7977
# difference between the two images is less than the epsilon value,
8078
# then we're going to accept that it's a reasonable lossy version of
81-
# the image. The old lena images for WebP are showing ~16 on
82-
# Ubuntu, the jpegs are showing ~18.
83-
target = hopper(self.rgb_mode)
84-
assert_image_similar(image, target, 12.0)
79+
# the image.
80+
target = hopper(mode)
81+
if mode != self.rgb_mode:
82+
target = target.convert(self.rgb_mode)
83+
assert_image_similar(image, target, epsilon)
84+
85+
def test_write_rgb(self, tmp_path):
86+
"""
87+
Can we write a RGB mode file to webp without error?
88+
Does it have the bits we expect?
89+
"""
90+
91+
self._roundtrip(tmp_path, self.rgb_mode, 12.5)
92+
93+
def test_write_method(self, tmp_path):
94+
self._roundtrip(tmp_path, self.rgb_mode, 12.0, {"method": 6})
95+
96+
buffer_no_args = io.BytesIO()
97+
hopper().save(buffer_no_args, format="WEBP")
98+
99+
buffer_method = io.BytesIO()
100+
hopper().save(buffer_method, format="WEBP", method=6)
101+
assert buffer_no_args.getbuffer() != buffer_method.getbuffer()
85102

86103
def test_write_unsupported_mode_L(self, tmp_path):
87104
"""
88105
Saving a black-and-white file to WebP format should work, and be
89106
similar to the original file.
90107
"""
91108

92-
temp_file = str(tmp_path / "temp.webp")
93-
hopper("L").save(temp_file)
94-
with Image.open(temp_file) as image:
95-
assert image.mode == self.rgb_mode
96-
assert image.size == (128, 128)
97-
assert image.format == "WEBP"
98-
99-
image.load()
100-
image.getdata()
101-
target = hopper("L").convert(self.rgb_mode)
102-
103-
assert_image_similar(image, target, 10.0)
109+
self._roundtrip(tmp_path, "L", 10.0)
104110

105111
def test_write_unsupported_mode_P(self, tmp_path):
106112
"""
107113
Saving a palette-based file to WebP format should work, and be
108114
similar to the original file.
109115
"""
110116

111-
temp_file = str(tmp_path / "temp.webp")
112-
hopper("P").save(temp_file)
113-
with Image.open(temp_file) as image:
114-
assert image.mode == self.rgb_mode
115-
assert image.size == (128, 128)
116-
assert image.format == "WEBP"
117-
118-
image.load()
119-
image.getdata()
120-
target = hopper("P").convert(self.rgb_mode)
121-
122-
assert_image_similar(image, target, 50.0)
117+
self._roundtrip(tmp_path, "P", 50.0)
123118

124119
def test_WebPEncode_with_invalid_args(self):
125120
"""

Tests/test_imagefont.py

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
assert_image_equal,
1414
assert_image_similar,
1515
assert_image_similar_tofile,
16-
is_mingw,
1716
is_pypy,
1817
is_win32,
1918
skip_unless_feature,
@@ -661,7 +660,22 @@ def test_variation_get(self):
661660
{"name": b"Size", "minimum": 0, "maximum": 300, "default": 0}
662661
]
663662

664-
@pytest.mark.skipif(is_mingw(), reason="epsilon too high for meaningful test")
663+
def _check_text(self, font, path, epsilon):
664+
im = Image.new("RGB", (100, 75), "white")
665+
d = ImageDraw.Draw(im)
666+
d.text((10, 10), "Text", font=font, fill="black")
667+
668+
try:
669+
with Image.open(path) as expected:
670+
assert_image_similar(im, expected, epsilon)
671+
except AssertionError:
672+
if "_adobe" in path:
673+
path = path.replace("_adobe", "_adobe_older_harfbuzz")
674+
with Image.open(path) as expected:
675+
assert_image_similar(im, expected, epsilon)
676+
else:
677+
raise
678+
665679
def test_variation_set_by_name(self):
666680
font = self.get_font()
667681

@@ -674,27 +688,18 @@ def test_variation_set_by_name(self):
674688
with pytest.raises(OSError):
675689
font.set_variation_by_name("Bold")
676690

677-
def _check_text(font, path, epsilon):
678-
im = Image.new("RGB", (100, 75), "white")
679-
d = ImageDraw.Draw(im)
680-
d.text((10, 10), "Text", font=font, fill="black")
681-
682-
with Image.open(path) as expected:
683-
assert_image_similar(im, expected, epsilon)
684-
685691
font = ImageFont.truetype("Tests/fonts/AdobeVFPrototype.ttf", 36)
686-
_check_text(font, "Tests/images/variation_adobe.png", 11)
692+
self._check_text(font, "Tests/images/variation_adobe.png", 11)
687693
for name in ["Bold", b"Bold"]:
688694
font.set_variation_by_name(name)
689-
_check_text(font, "Tests/images/variation_adobe_name.png", 11)
695+
self._check_text(font, "Tests/images/variation_adobe_name.png", 11)
690696

691697
font = ImageFont.truetype("Tests/fonts/TINY5x3GX.ttf", 36)
692-
_check_text(font, "Tests/images/variation_tiny.png", 40)
698+
self._check_text(font, "Tests/images/variation_tiny.png", 40)
693699
for name in ["200", b"200"]:
694700
font.set_variation_by_name(name)
695-
_check_text(font, "Tests/images/variation_tiny_name.png", 40)
701+
self._check_text(font, "Tests/images/variation_tiny_name.png", 40)
696702

697-
@pytest.mark.skipif(is_mingw(), reason="epsilon too high for meaningful test")
698703
def test_variation_set_by_axes(self):
699704
font = self.get_font()
700705

@@ -707,21 +712,13 @@ def test_variation_set_by_axes(self):
707712
with pytest.raises(OSError):
708713
font.set_variation_by_axes([500, 50])
709714

710-
def _check_text(font, path, epsilon):
711-
im = Image.new("RGB", (100, 75), "white")
712-
d = ImageDraw.Draw(im)
713-
d.text((10, 10), "Text", font=font, fill="black")
714-
715-
with Image.open(path) as expected:
716-
assert_image_similar(im, expected, epsilon)
717-
718715
font = ImageFont.truetype("Tests/fonts/AdobeVFPrototype.ttf", 36)
719716
font.set_variation_by_axes([500, 50])
720-
_check_text(font, "Tests/images/variation_adobe_axes.png", 5.1)
717+
self._check_text(font, "Tests/images/variation_adobe_axes.png", 5.1)
721718

722719
font = ImageFont.truetype("Tests/fonts/TINY5x3GX.ttf", 36)
723720
font.set_variation_by_axes([100])
724-
_check_text(font, "Tests/images/variation_tiny_axes.png", 32.5)
721+
self._check_text(font, "Tests/images/variation_tiny_axes.png", 32.5)
725722

726723

727724
@skip_unless_feature("raqm")

Tests/test_imageshow.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,21 @@ def test_register():
1717
ImageShow._viewers.pop()
1818

1919

20-
def test_viewer_show():
20+
@pytest.mark.parametrize(
21+
"order", [-1, 0],
22+
)
23+
def test_viewer_show(order):
2124
class TestViewer(ImageShow.Viewer):
22-
methodCalled = False
23-
2425
def show_image(self, image, **options):
2526
self.methodCalled = True
2627
return True
2728

2829
viewer = TestViewer()
29-
ImageShow.register(viewer, -1)
30+
ImageShow.register(viewer, order)
3031

3132
for mode in ("1", "I;16", "LA", "RGB", "RGBA"):
32-
with hopper() as im:
33+
viewer.methodCalled = False
34+
with hopper(mode) as im:
3335
assert ImageShow.show(im)
3436
assert viewer.methodCalled
3537

0 commit comments

Comments
 (0)