pyvips icon indicating copy to clipboard operation
pyvips copied to clipboard

Can't find a way to check image file type.

Open 2h4dl opened this issue 5 years ago • 10 comments

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.

2h4dl avatar Sep 30 '19 08:09 2h4dl

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).

jcupitt avatar Sep 30 '19 09:09 jcupitt

Hi @jcupitt, is there any way else I can do to get file format exactlly, like what PIL.Image.format does?

2h4dl avatar Oct 01 '19 00:10 2h4dl

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?

jcupitt avatar Oct 01 '19 06:10 jcupitt

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.

2h4dl avatar Oct 04 '19 11:10 2h4dl

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"

jcupitt avatar Oct 04 '19 12:10 jcupitt

Haha, not convinced, but work. Thank you @jcupitt.

2h4dl avatar Oct 04 '19 12:10 2h4dl

@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?

LoyalPotato avatar Mar 21 '24 11:03 LoyalPotato

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.

jcupitt avatar Mar 21 '24 11:03 jcupitt

Understandable 🙂

Thank you!

LoyalPotato avatar Mar 21 '24 11:03 LoyalPotato