Skip to content

Commit

Permalink
Refactor image loading: use context managers for safer file handling
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreRaybaut committed Nov 7, 2024
1 parent f923828 commit 9df9757
Showing 1 changed file with 49 additions and 49 deletions.
98 changes: 49 additions & 49 deletions plotpy/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,55 +284,55 @@ def _imread_pil(filename, to_grayscale=False, **kwargs):
"I;16",
"I;16",
)
img = PIL.Image.open(filename)
base, ext = osp.splitext(filename)
ext = ext.lower()
if ext in [".tif", ".tiff"]:
# try to know if multiple pages
nb_pages = 0
while True:
try:
img.seek(nb_pages)
nb_pages += 1
except EOFError:
break
if nb_pages > 1:
for i in range(nb_pages):
img.seek(i)
filename = base
filename += "_{i:d}".format(i=i)
filename += ext
img.save(filename)
if nb_pages == 2:
# possibility to be TIFF file with thumbnail and full image
# --> try to load full image (second one)
filename = base + "_{i:d}".format(i=1) + ext
else:
# we don't know which one must be loaded --> load first image
filename = base + "_{i:d}".format(i=0) + ext

img = PIL.Image.open(filename)
if img.mode in ("CMYK", "YCbCr"):
# Converting to RGB
img = img.convert("RGB")
if to_grayscale and img.mode in ("RGB", "RGBA", "RGBX"):
# Converting to grayscale
img = img.convert("L")
elif "A" in img.mode or (img.mode == "P" and "transparency" in img.info):
img = img.convert("RGBA")
elif img.mode == "P":
img = img.convert("RGB")
try:
dtype, extra = DTYPES[img.mode]
except KeyError:
raise RuntimeError(f"{img.mode} mode is not supported")
shape = (img.size[1], img.size[0])
if extra is not None:
shape += (extra,)
try:
return np.array(img, dtype=np.dtype(dtype)).reshape(shape)
except SystemError:
return np.array(img.getdata(), dtype=np.dtype(dtype)).reshape(shape)
with PIL.Image.open(filename) as img:
base, ext = osp.splitext(filename)
ext = ext.lower()
if ext in [".tif", ".tiff"]:
# try to know if multiple pages
nb_pages = 0
while True:
try:
img.seek(nb_pages)
nb_pages += 1
except EOFError:
break
if nb_pages > 1:
for i in range(nb_pages):
img.seek(i)
filename = base
filename += "_{i:d}".format(i=i)
filename += ext
img.save(filename)
if nb_pages == 2:
# possibility to be TIFF file with thumbnail and full image
# --> try to load full image (second one)
filename = base + "_{i:d}".format(i=1) + ext
else:
# we don't know which one must be loaded --> load first image
filename = base + "_{i:d}".format(i=0) + ext

with PIL.Image.open(filename) as img:
if img.mode in ("CMYK", "YCbCr"):
# Converting to RGB
img = img.convert("RGB")
if to_grayscale and img.mode in ("RGB", "RGBA", "RGBX"):
# Converting to grayscale
img = img.convert("L")
elif "A" in img.mode or (img.mode == "P" and "transparency" in img.info):
img = img.convert("RGBA")
elif img.mode == "P":
img = img.convert("RGB")
try:
dtype, extra = DTYPES[img.mode]
except KeyError:
raise RuntimeError(f"{img.mode} mode is not supported")
shape = (img.size[1], img.size[0])
if extra is not None:
shape += (extra,)
try:
return np.array(img, dtype=np.dtype(dtype)).reshape(shape)
except SystemError:
return np.array(img.getdata(), dtype=np.dtype(dtype)).reshape(shape)


def _imwrite_pil(filename, arr):
Expand Down

0 comments on commit 9df9757

Please sign in to comment.