Fast way to get image dimensions (not filesize)

前端 未结 8 1938
独厮守ぢ
独厮守ぢ 2021-01-29 23:03

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

8条回答
  •  你的背包
    2021-01-29 23:04

    You can use ImageMagick's identify function. Here's how you do it in bash (Note $0 is the image's path):

    width=$(identify -format "%w" "$0")> /dev/null
    height=$(identify -format "%h" "$0")> /dev/null
    

    And this also hides any potential error messages. Modern implementations of identify only read the header, not the whole image, so it is fast. Not sure how it compares to other methods though.

提交回复
热议问题