简体   繁体   English

是否打算在Python中尽快获取图像的高度和宽度(jpg,原始文件,tiff)?

[英]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? 如何在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渲染图像并计算高度和宽度,但是我想知道是否可以先使用EXIF数据或某些东西(exiftool)或其他嵌入的元数据,并且只能将PIL作为最后的手段?

PIL already tries to give you this information efficiently. PIL 已经尝试为您提供有效的信息。 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. 如果您只需要从图像中获取信息,那么PIL只会读取获取元数据信息所需的尽可能多的数据。

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. 如果您遵守ZPL许可证 ,则可以仅处理GIF,JPG和PNG图像的Zope代码库改编此功能 I've had a hand in that code at some point in the distant past, somewhere in the Python 1.x days. 我曾经在遥远的某个时候(在Python 1.x时代的某个地方)使用过该代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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