Given an image file's name and location as a string, I would like to definitively determine what the image type is e.g PNG, JPG, BMP etc.
Asked
Active
Viewed 226 times
-2
-
2Would the file extension definitively decide what the file type is? What if the image is a PNG and the user renamed the file to .jpg? – TheShield Jun 22 '19 at 17:24
-
1While you can do that via extension or other methods like looking at magic numbers/headers, I do not think it's ever 100% possible to determine a file type. Tools like `file` on Linux attempt to do this but again it's just a heuristic sort of guess usually. It's only possible if you know for sure those are the only types that will be given to you in a valid format as otherwise for all you know the file might just be random binary data that coincidentally looks like an image in its header structure. – Lemon Drop Jun 22 '19 at 17:25
-
2We have applications that work with images and customers do attempt to use mis-labeled images now and then (PNG image with JPG file name, etc.), so it's better to use a library that checks the header data in the file instead of just looking at the extension. – Dave S Jun 22 '19 at 17:27
-
Why can't you extract the letters followed by last dot, and match it whether it is PNG, JPG, or BMP.. etc. If the extension is not present for some reason, you can look up the file signature and find out the type if the file is valid. See [https://en.wikipedia.org/wiki/List_of_file_signatures]. – prime_hit Jun 22 '19 at 17:27
-
@TheShield if a user appends the name of a `png` file with `.jpg` its extension is still `png`. If you're talking about a situation where a user takes, for example, a HTML file and changes the extension to, for example, .png, the file is unreadable but **it is now** a `png` file. Do you want to deduce the extension by file contents? Well, what if someone creates a `txt` with `c` code? Should it be a `txt` or a `c` file? – Fureeish Jun 22 '19 at 17:29
-
Quite a few image file types are containers for other image types (tiff springs to mind). – Richard Critten Jun 22 '19 at 17:36
1 Answers
1
I have decided to settle with ImageMagick's identify -format "%m" filename
command as it handles the case where a user could change the extension of the file to another image, yet the contents remain the same.
Example run with an image called test.png
whose contents are in the PNG format:
identify -format "%m" test.png
PNG
mv test.png test.jpg
identify -format "%m" test.jpg
PNG
Which is the result I was looking for.

TheShield
- 297
- 1
- 5
- 18