简体   繁体   中英

Strategy to get the height and width of an image (jpg, raw file, tiff) as quickly as possible in Python?

How can I get the height and width of an image as quickly as possible in Python?

I know I can use PIL to render the image and calculate the height and width, but I'm wondering if I can use EXIF data or something (exiftool), or some other embedded metadata first, and only use PIL as a last resort?

PIL already tries to give you this information efficiently. You can safely use it to extract image sizes without it trying to read the whole file. PIL will only read as much data as needed to obtain metadata information, if that is all you wanted from the image.

In theory, you can be more efficient still, to load only the width and height information from an image. You'd have to code this yourself, which is not that hard if you read up on the image formats. That's the real problem, you'll have to reinvent the binary image data reading wheel.

If you adhere to the ZPL license , you could perhaps adapt this function from the Zope codebase which handles just GIF, JPG and PNG images. I've had a hand in that code at some point in the distant past, somewhere in the Python 1.x days.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM