Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checking file's content type instead of extension #1171

Merged
merged 1 commit into from
Apr 7, 2024

Conversation

serengil
Copy link
Owner

@serengil serengil commented Apr 7, 2024

Tickets

#1169

What has been done

With this PR, we are now checking the file's content type instead of file's extension while we are loading facial database.

How to test

make lint && make test

@@ -3,6 +3,7 @@
import pickle
from typing import List, Union, Optional, Dict, Any
import time
import imghdr
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I discourage the use of imghdr as already shows "deprecated" and support will be removed soon. Maybe PIL is better

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot

Copy link
Owner Author

@serengil serengil Apr 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommended way to determine file type with PIL is

from PIL import Image
img = Image.open(filename)
print(img.format)

But I do not want to load and open file. If this is the way, then I will prefer to use file extension.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to read carefully the PIL documentation for the open method on image:

This is a lazy operation; this function identifies the file, but the file remains open and the actual image data is not read from the file until you try to process the data (or call the :py:meth:~PIL.Image.Image.load method).

Also img.format reads only as many bytes needed to parse the header (pretty much like imghder.what) hence the operation is very cheap.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cheers!

@serengil serengil merged commit ff27c74 into master Apr 7, 2024
4 checks passed
@serengil serengil mentioned this pull request Apr 7, 2024
@serengil serengil deleted the feat-task-0704-file-extensions branch April 7, 2024 08:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants