mirror of
https://github.com/serengil/deepface.git
synced 2025-06-07 12:05:22 +00:00
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
This commit is contained in:
commit
42ee2982f0
@ -88,13 +88,12 @@ def load_base64(uri: str) -> np.ndarray:
|
|||||||
encoded_data = encoded_data_parts[1]
|
encoded_data = encoded_data_parts[1]
|
||||||
decoded_bytes = base64.b64decode(encoded_data)
|
decoded_bytes = base64.b64decode(encoded_data)
|
||||||
|
|
||||||
img = Image.open(io.BytesIO(decoded_bytes))
|
|
||||||
file_type = img.format.lower()
|
|
||||||
|
|
||||||
# similar to find functionality, we are just considering these extensions
|
# similar to find functionality, we are just considering these extensions
|
||||||
# content type is safer option than file extension
|
# content type is safer option than file extension
|
||||||
if file_type not in ["jpeg", "png"]:
|
with Image.open(io.BytesIO(decoded_bytes)) as img:
|
||||||
raise ValueError(f"input image can be jpg or png, but it is {file_type}")
|
file_type = img.format.lower()
|
||||||
|
if file_type not in ["jpeg", "png"]:
|
||||||
|
raise ValueError(f"input image can be jpg or png, but it is {file_type}")
|
||||||
|
|
||||||
nparr = np.fromstring(decoded_bytes, np.uint8)
|
nparr = np.fromstring(decoded_bytes, np.uint8)
|
||||||
img_bgr = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
img_bgr = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
||||||
|
@ -305,11 +305,9 @@ def __list_images(path: str) -> List[str]:
|
|||||||
if ext_lower not in {".jpg", ".jpeg", ".png"}:
|
if ext_lower not in {".jpg", ".jpeg", ".png"}:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
img = Image.open(exact_path) # lazy
|
with Image.open(exact_path) as img: # lazy
|
||||||
|
if img.format.lower() in ["jpeg", "png"]:
|
||||||
file_type = img.format.lower()
|
images.append(exact_path)
|
||||||
if file_type in ["jpeg", "png"]:
|
|
||||||
images.append(exact_path)
|
|
||||||
return images
|
return images
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user