Skip to content

Commit 42ee298

Browse files
authored
Merge pull request #1174 from serengil/feat-task-0704-determine-file-type-with-pil
using with clause to open img with pil to guarantee it is closed
2 parents ba4d193 + 83c381e commit 42ee298

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

deepface/modules/preprocessing.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,12 @@ def load_base64(uri: str) -> np.ndarray:
8888
encoded_data = encoded_data_parts[1]
8989
decoded_bytes = base64.b64decode(encoded_data)
9090

91-
img = Image.open(io.BytesIO(decoded_bytes))
92-
file_type = img.format.lower()
93-
9491
# similar to find functionality, we are just considering these extensions
9592
# content type is safer option than file extension
96-
if file_type not in ["jpeg", "png"]:
97-
raise ValueError(f"input image can be jpg or png, but it is {file_type}")
93+
with Image.open(io.BytesIO(decoded_bytes)) as img:
94+
file_type = img.format.lower()
95+
if file_type not in ["jpeg", "png"]:
96+
raise ValueError(f"input image can be jpg or png, but it is {file_type}")
9897

9998
nparr = np.fromstring(decoded_bytes, np.uint8)
10099
img_bgr = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

deepface/modules/recognition.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,9 @@ def __list_images(path: str) -> List[str]:
305305
if ext_lower not in {".jpg", ".jpeg", ".png"}:
306306
continue
307307

308-
img = Image.open(exact_path) # lazy
309-
310-
file_type = img.format.lower()
311-
if file_type in ["jpeg", "png"]:
312-
images.append(exact_path)
308+
with Image.open(exact_path) as img: # lazy
309+
if img.format.lower() in ["jpeg", "png"]:
310+
images.append(exact_path)
313311
return images
314312

315313

0 commit comments

Comments
 (0)