Thanks, I tried python-filetype but that did not solve the problem. I ended up installing python-deadlib which includes python-imghdr and that solved the problem.
Some additional info that may or may not be of interest.
Instead of using old code with no maintainers, I think it’s better to use PIL (the fork Pillow):
>>> import os
>>> from PIL import Image
>>> i = Image.open(os.path.expanduser("~/my-unknown-image"))
>>> i.info
{'jfif': 257, 'jfif_version': (1, 1), 'jfif_unit': 0, 'jfif_density': (1, 1)}
You can also (at least quite often) read off a few bytes in binary mode and get the info from there. It all depends on what you are looking for. Suffice it to say, that PIL can read LOTS of formats (https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html), though I think it currently doesn’t have support for avif, but it may have changed recently, but if it hasn’t an additional module needs to be installed, such as pillow_avif, which I use.