8000 Fix Pillow import in testing. · matplotlib/matplotlib@3a1fbf3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3a1fbf3

Browse files
committed
Fix Pillow import in testing.
The expected import is `from PIL import Image`, and if you import the compare function directly, it will fail: ``` >>> from matplotlib.testing.compare import compare_images >>> compare_images('a.png', 'b.png', 0) Traceback (most recent call last): File "<stdin>", line 1, in <module> File ".../matplotlib/testing/compare.py", line 394, in compare_images expected_image = np.asarray(PIL.Image.open(expected).convert("RGB")) AttributeError: module 'PIL' has no attribute 'Image' ``` It only works in our test suite because some other file does it correctly.
1 parent df67aaf commit 3a1fbf3

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

lib/matplotlib/testing/compare.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from tempfile import TemporaryDirectory, TemporaryFile
1414

1515
import numpy as np
16-
import PIL
16+
from PIL import Image
1717

1818
import matplotlib as mpl
1919
from matplotlib import cbook
@@ -391,8 +391,8 @@ def compare_images(expected, actual, tol, in_decorator=False):
391391
expected = convert(expected, cache=True)
392392

393393
# open the image files and remove the alpha channel (if it exists)
394-
expected_image = np.asarray(PIL.Image.open(expected).convert("RGB"))
395-
actual_image = np.asarray(PIL.Image.open(actual).convert("RGB"))
394+
expected_image = np.asarray(Image.open(expected).convert("RGB"))
395+
actual_image = np.asarray(Image.open(actual).convert("RGB"))
396396

397397
actual_image, expected_image = crop_to_same(
398398
actual, actual_image, expected, expected_image)
@@ -442,8 +442,8 @@ def save_diff_image(expected, actual, output):
442442
File path to save difference image to.
443443
"""
444444
# Drop alpha channels, similarly to compare_images.
445-
expected_image = np.asarray(PIL.Image.open(expected).convert("RGB"))
446-
actual_image = np.asarray(PIL.Image.open(actual).convert("RGB"))
445+
expected_image = np.asarray(Image.open(expected).convert("RGB"))
446+
actual_image = np.asarray(Image.open(actual).convert("RGB"))
447447
actual_image, expected_image = crop_to_same(
448448
actual, actual_image, expected, expected_image)
449449
expected_image = np.array(expected_image).astype(float)
@@ -469,4 +469,4 @@ def save_diff_image(expected, actual, output):
469469
# Hard-code the alpha channel to fully solid
470470
save_image_np[:, :, 3] = 255
471471

472-
PIL.Image.fromarray(save_image_np).save(output, format="png")
472+
Image.fromarray(save_image_np).save(output, format="png")

0 commit comments

Comments
 (0)
0