Skip to content

Commit cd8c0c3

Browse files
colesburysoumith
authored andcommitted
Fix ResourceWarning due to Image.open(path) (#176)
See python-pillow/Pillow#835
1 parent f4c4d6c commit cd8c0c3

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

torchvision/datasets/folder.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ def make_dataset(dir, class_to_idx):
3939

4040

4141
def pil_loader(path):
42-
return Image.open(path).convert('RGB')
42+
# open path as file to avoid ResourceWarning (https://github.com/python-pillow/Pillow/issues/835)
43+
with open(path, 'rb') as f:
44+
with Image.open(f) as img:
45+
return img.convert('RGB')
4346

4447

4548
def accimage_loader(path):

0 commit comments

Comments
 (0)