快速获取图像尺寸的方法(不是文件大小) [英] Fast way to get image dimensions (not filesize)

查看:140
本文介绍了快速获取图像尺寸的方法(不是文件大小)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种快速方式来获取图像的高度和宽度(以像素为单位)。它应该至少处理JPG,PNG和TIFF,但越多越好。我强调快速,因为我的图像非常大(高达250 MB)并且使用ImageMagick的识别来获得大小需要太长时间,因为它显然首先读取整个图像。

I'm looking for a fast way to get the height and width of an image in pixels. It should handle at least JPG, PNG and TIFF, but the more the better. I emphasize fast because my images are quite big (up to 250 MB) and it takes soooo long to get the size with ImageMagick's identify because it obviously reads the images as a whole first.

优选地,我寻找一种在Ruby中,甚至在Rails 3中都能很好地工作的方法。

Preferably, I look for a way that works well in Ruby, or even in Rails 3.

我知道理论的东西(各种图像格式,它们的标题和它们的差异等等)。实际上,我要求某种类型的库可以通过相当通用的方式解决我的问题。

I know the theory stuff (various image formats, their headers and their differences, and so on). Indeed, I ask for some kind of library that can resolve my issue in a fairly generic way.

我刚发现 http://imagesize.rubyforge.org 看起来很有希望虽然开发似乎已经死了。

I just found http://imagesize.rubyforge.org which looks promising although development seems to be dead.

推荐答案


  • 文件命令打印多种图像格式的尺寸(例如PNG,GIF,JPEG;但不是PPM,WEBP),并且只读取标题。

    • The file command prints the dimensions for several image formats (e.g. PNG, GIF, JPEG; but not PPM, WEBP), and does only read the header.

      exiv2 为您提供多种格式的维度,包括JPEG,TIFF,PNG,GIF,WEBP,即使没有EXIF标题存在。目前还不清楚它是否会读取整个数据。有关所有支持的图像格式,请参阅exiv2的联机帮助页。

      exiv2 gives you dimensions for many formats, including JPEG, TIFF, PNG, GIF, WEBP, even if no EXIF header present. It is unclear if it reads the whole data for that though. See the manpage of exiv2 for all supported image formats.

      标识命令(来自ImageMagick)打印大量图像信息适用于各种图像。它似乎限制自己阅读标题部分(见评论)。

      The identify command (from ImageMagick) prints lots of image information for a wide variety of images. It seems to restrain itself to reading the header portion (see comments).

      head -n1 将为您提供PPM,PGM格式的尺寸。

      head -n1 will give you the dimensions for PPM, PGM formats.

      对于网络上流行的格式, exiv2 识别将完成这项工作。
      根据用例的不同,您可能需要编写自己的脚本来组合/解析多个工具的输出。

      For formats popular on the web, both exiv2 and identify will do the job. Depending on the use-case you may need to write your own script that combines/parses outputs of several tools.

      这篇关于快速获取图像尺寸的方法(不是文件大小)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆