pyvips
pyvips copied to clipboard
Can't find a way to check image file type.
Can't find a safe way to get image file type, e.g jpg, png, bmp or other type.
I tried get metadata with pyvips.Image.get_fields
and pyvips.Image.get("vips_loader")
,
but worried about its reliablity.
Hello @2h4dl,
Yes, image.get("vips-loader")
should reliably tell you which loader loaded the file. It's not always precise though --- magickload
, for example, can mean any format supported by ImageMagick (or GraphicsMagick).
Hi @jcupitt, is there any way else I can do to get file format exactlly, like what PIL.Image.format does?
The libvips file type sniffers are all designed around picking a loader, so all you can really get is the name of the load class. You can introspect that in turn to get other information, like allowed extensions.
Could you explain what do you need to get from pyvips to solve your problem? Do you need something like a MIME type?
Sorry @jcupitt , I don't know what MIME is. I need to know image format from internet buffer loaded by pyvips.Image.new_from_buffer.
I would use something like:
def format(image):
formats = {
"jpegload": "JPEG",
"tiffload": "TIFF",
etc.
}
loader = image.get("vips-loader")
return formats[loader] if loader in formats else "unknown"
Haha, not convinced, but work. Thank you @jcupitt.
@jcupitt in your comment you mention that this method is not always accurate, giving the example of magickload
.
Since the time of writing, has another way of detecting the image file type been introduced?
I think file type detection is out of scope for libvips. It's a hard problem to solve well and we have enough on our plate already heh.
I would look into something like exiftool.
Understandable 🙂
Thank you!