From 1d957146e7f729914b64cc42d4912dbd359be348 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 10 Sep 2020 22:52:08 +1000 Subject: [PATCH] Fixed loading profile with non-Unicode path on Windows --- Tests/test_imagecms.py | 14 ++++++++++++++ src/PIL/ImageCms.py | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/Tests/test_imagecms.py b/Tests/test_imagecms.py index e9149b84332..f037d1f95b5 100644 --- a/Tests/test_imagecms.py +++ b/Tests/test_imagecms.py @@ -1,6 +1,7 @@ import datetime import os import re +import shutil from io import BytesIO import pytest @@ -436,6 +437,19 @@ def truncate_tuple(tuple_or_float): assert p.xcolor_space == "RGB " +def test_non_unicode_path(tmp_path): + skip_missing() + try: + tempfile = str(tmp_path / ("temp_" + chr(128) + ".icc")) + except UnicodeEncodeError: + pytest.skip("Non-Unicode path could not be created") + shutil.copy(SRGB, tempfile) + + o = ImageCms.getOpenProfile(tempfile) + p = o.profile + assert p.model == "IEC 61966-2-1 Default RGB Colour Space - sRGB" + + def test_profile_typesafety(): """Profile init type safety diff --git a/src/PIL/ImageCms.py b/src/PIL/ImageCms.py index 3856cb84342..8c4740ddcb1 100644 --- a/src/PIL/ImageCms.py +++ b/src/PIL/ImageCms.py @@ -159,6 +159,14 @@ def __init__(self, profile): """ if isinstance(profile, str): + if sys.platform == "win32": + profile_bytes_path = profile.encode() + try: + profile_bytes_path.decode("ascii") + except UnicodeDecodeError: + with open(profile, "rb") as f: + self._set(core.profile_frombytes(f.read())) + return self._set(core.profile_open(profile), profile) elif hasattr(profile, "read"): self._set(core.profile_frombytes(profile.read()))