Skip to content

Commit 2091365

Browse files
Erotemicsoumith
authored andcommitted
Improved image extension robustness (#322)
is_image_file is now case insensitive Added docstring to is_image_file is_image_file now returns true for pgm files
1 parent 15f6a22 commit 2091365

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

torchvision/datasets/folder.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44
import os
55
import os.path
66

7-
IMG_EXTENSIONS = [
8-
'.jpg', '.JPG', '.jpeg', '.JPEG',
9-
'.png', '.PNG', '.ppm', '.PPM', '.bmp', '.BMP',
10-
]
7+
IMG_EXTENSIONS = ['.jpg', '.jpeg', '.png', '.ppm', '.bmp', '.pgm']
118

129

1310
def is_image_file(filename):
14-
return any(filename.endswith(extension) for extension in IMG_EXTENSIONS)
11+
"""Checks if a file is an image.
12+
13+
Args:
14+
filename (string): path to a file
15+
16+
Returns:
17+
bool: True if the filename ends with a known image extension
18+
"""
19+
filename_lower = filename.lower()
20+
return any(filename_lower.endswith(ext) for ext in IMG_EXTENSIONS)
1521

1622

1723
def find_classes(dir):

0 commit comments

Comments
 (0)