This answers the topic question. It tests if the file ext matches the supported image file formats.
But what about actually testing if the file is an actual image file and not, let say, a text file with a .bmp extension?
Is there any means to do this or is it better to just use a try except block?
Thanks
Each "Image"-Format has a specific header. You could open the file, and check for the Headers.
for your example "textfile with bmp-Extension":
You would expect "Bitmap"-Format
"Bitmap"-Format Header = 0x42 0x4D (
https://en.wikipedia.org/wiki/BMP_file_format)
If the first 2 Bytes are 0x42 0x4D then it's a valid Bitmap, if no.....
OTOH, if your Textfile actually starts with exactly those values you're back at square one.
Bottom Line: I don't think you can avoid exception-handling (try/Except)