Skip to content

Commit 4ace920

Browse files
committed
Move version logic into function; do error checking on it
1 parent 1594569 commit 4ace920

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

torchvision/utils.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import torch
1111
from PIL import __version__ as PILLOW_VERSION_STRING, Image, ImageColor, ImageDraw, ImageFont
1212

13-
PILLOW_VERSION = tuple(int(x) for x in PILLOW_VERSION_STRING.split("."))
1413

1514
__all__ = [
1615
"_Image_fromarray",
@@ -185,7 +184,16 @@ def _Image_fromarray(
185184
mode paramter. See:
186185
https://pillow.readthedocs.io/en/stable/releasenotes/11.3.0.html#image-fromarray-mode-parameter
187186
"""
188-
if PILLOW_VERSION >= (11, 3):
187+
188+
# This may throw if the version string is from an install that comes from a
189+
# non-stable or development version. We'll fall back to the old behavior in
190+
# such cases.
191+
try:
192+
PILLOW_VERSION = tuple(int(x) for x in PILLOW_VERSION_STRING.split("."))
193+
except Exception:
194+
PILLOW_VERSION = None
195+
196+
if PILLOW_VERSION is not None and PILLOW_VERSION >= (11, 3):
189197
# The actual PR that implements the deprecation has more context for why
190198
# it was done, and also points out some problems:
191199
#

0 commit comments

Comments
 (0)