CLI image-folder regex missing end-anchor picks up non-image files (foo.jpg.bak, notes.jpegdoc)
Author: chirag127Created Jul 4, 2026Updated Aug 1, 2026
Problem
image_files_in_folder uses an unanchored regex, so files like photo.jpg.bak, notes.jpegdoc, and archive.tar.jpg.gz are treated as images and passed to PIL, which then fails on load.
File
face_recognition/face_recognition_cli.py:68face_recognition/face_detection_cli.py:26
Both contain:
return [os.path.join(folder, f) for f in os.listdir(folder) if re.match(r'.*\.(jpg|jpeg|png)', f, flags=re.I)]
Steps
$ mkdir known && touch known/photo.jpg.bak known/notes.jpegdoc
$ python3 -c "import re; files=['photo.jpg.bak','notes.jpegdoc','archive.tar.jpg.gz']; print([bool(re.match(r'.*\.(jpg|jpeg|png)', f, re.I)) for f in files])"
[True, True, True]
Expected
Regex matches only real image extensions at end of filename.
Actual
re.match anchors at start only; missing $ makes any string containing .jpg/.jpeg/.png match. Fix: re.match(r'.*\.(jpg|jpeg|png)$', f, re.I) or use os.path.splitext.
Environment
face_recognition 1.4.0 (setup.py), Python 3.5-3.9, dlib>=19.7, master @ 9f3061aa (2026-06-25).
Thanks for maintaining ageitgey/face_recognition!
Source: ageitgey/face_recognition