Skip to content

Commit 9df9757

Browse files
committed
Refactor image loading: use context managers for safer file handling
1 parent f923828 commit 9df9757

File tree

1 file changed

+49
-49
lines changed

1 file changed

+49
-49
lines changed

plotpy/io.py

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -284,55 +284,55 @@ def _imread_pil(filename, to_grayscale=False, **kwargs):
284284
"I;16",
285285
"I;16",
286286
)
287-
img = PIL.Image.open(filename)
288-
base, ext = osp.splitext(filename)
289-
ext = ext.lower()
290-
if ext in [".tif", ".tiff"]:
291-
# try to know if multiple pages
292-
nb_pages = 0
293-
while True:
294-
try:
295-
img.seek(nb_pages)
296-
nb_pages += 1
297-
except EOFError:
298-
break
299-
if nb_pages > 1:
300-
for i in range(nb_pages):
301-
img.seek(i)
302-
filename = base
303-
filename += "_{i:d}".format(i=i)
304-
filename += ext
305-
img.save(filename)
306-
if nb_pages == 2:
307-
# possibility to be TIFF file with thumbnail and full image
308-
# --> try to load full image (second one)
309-
filename = base + "_{i:d}".format(i=1) + ext
310-
else:
311-
# we don't know which one must be loaded --> load first image
312-
filename = base + "_{i:d}".format(i=0) + ext
313-
314-
img = PIL.Image.open(filename)
315-
if img.mode in ("CMYK", "YCbCr"):
316-
# Converting to RGB
317-
img = img.convert("RGB")
318-
if to_grayscale and img.mode in ("RGB", "RGBA", "RGBX"):
319-
# Converting to grayscale
320-
img = img.convert("L")
321-
elif "A" in img.mode or (img.mode == "P" and "transparency" in img.info):
322-
img = img.convert("RGBA")
323-
elif img.mode == "P":
324-
img = img.convert("RGB")
325-
try:
326-
dtype, extra = DTYPES[img.mode]
327-
except KeyError:
328-
raise RuntimeError(f"{img.mode} mode is not supported")
329-
shape = (img.size[1], img.size[0])
330-
if extra is not None:
331-
shape += (extra,)
332-
try:
333-
return np.array(img, dtype=np.dtype(dtype)).reshape(shape)
334-
except SystemError:
335-
return np.array(img.getdata(), dtype=np.dtype(dtype)).reshape(shape)
287+
with PIL.Image.open(filename) as img:
288+
base, ext = osp.splitext(filename)
289+
ext = ext.lower()
290+
if ext in [".tif", ".tiff"]:
291+
# try to know if multiple pages
292+
nb_pages = 0
293+
while True:
294+
try:
295+
img.seek(nb_pages)
296+
nb_pages += 1
297+
except EOFError:
298+
break
299+
if nb_pages > 1:
300+
for i in range(nb_pages):
301+
img.seek(i)
302+
filename = base
303+
filename += "_{i:d}".format(i=i)
304+
filename += ext
305+
img.save(filename)
306+
if nb_pages == 2:
307+
# possibility to be TIFF file with thumbnail and full image
308+
# --> try to load full image (second one)
309+
filename = base + "_{i:d}".format(i=1) + ext
310+
else:
311+
# we don't know which one must be loaded --> load first image
312+
filename = base + "_{i:d}".format(i=0) + ext
313+
314+
with PIL.Image.open(filename) as img:
315+
if img.mode in ("CMYK", "YCbCr"):
316+
# Converting to RGB
317+
img = img.convert("RGB")
318+
if to_grayscale and img.mode in ("RGB", "RGBA", "RGBX"):
319+
# Converting to grayscale
320+
img = img.convert("L")
321+
elif "A" in img.mode or (img.mode == "P" and "transparency" in img.info):
322+
img = img.convert("RGBA")
323+
elif img.mode == "P":
324+
img = img.convert("RGB")
325+
try:
326+
dtype, extra = DTYPES[img.mode]
327+
except KeyError:
328+
raise RuntimeError(f"{img.mode} mode is not supported")
329+
shape = (img.size[1], img.size[0])
330+
if extra is not None:
331+
shape += (extra,)
332+
try:
333+
return np.array(img, dtype=np.dtype(dtype)).reshape(shape)
334+
except SystemError:
335+
return np.array(img.getdata(), dtype=np.dtype(dtype)).reshape(shape)
336336

337337

338338
def _imwrite_pil(filename, arr):

0 commit comments

Comments
 (0)