Skip to content

Commit a0b44f9

Browse files
committed
Fix ResourceWarning in ImageSlide.detect_format()
Explicitly close the PIL.Image to avoid an "unclosed file" warning.
1 parent f2bfe2f commit a0b44f9

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

openslide/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,12 @@ def detect_format(cls, filename):
277277
278278
If the file format is not recognized, return None."""
279279
try:
280-
return Image.open(filename).format
280+
img = Image.open(filename)
281+
format = img.format
282+
if hasattr(img, 'close'):
283+
# Pillow >= 2.5.0
284+
img.close()
285+
return format
281286
except IOError:
282287
return None
283288

0 commit comments

Comments
 (0)