在Linux终端获取图片信息(如宽、高)

日 16 三月 2014 by greatcl

有时候会需要查看图片的信息,专门打开一个图片软件来做这件事,真是会比较麻烦。其实在终端里就可以做这件事了。

file命令

0   Pictures $ file couple.png
couple.png: PNG image data, 1440 x 900, 8-bit/color RGB, non-interlaced

但是并非所有的图片都可以显示尺寸,JPEG格式的就不可以

0   Pictures $ file couple.jpg
couple.jpg: JPEG image data, JFIF standard 1.01, comment: "\377\376"

rdjpgcom 命令

相比file命令不能显示JPEG格式图片的尺寸,rdjpgcom命令则只能处理JPEG格式的文件

0   Pictures $ rdjpgcom -verbose smoke.jpg 
JPEG image is 1920w * 1200h, 3 color components, 8 bits per sample
JPEG process: Baseline

identify 命令

0   Pictures $ identify couple.png 
couple.png PNG 1440x900 1440x900+0+0 8-bit DirectClass 1.44MB 0.000u 0:00.000

identify命令对JPEG格式也是适用的

0   Pictures $ identify smoke.jpg 
smoke.jpg JPEG 1920x1200 1920x1200+0+0 8-bit DirectClass 328KB 0.010u 0:00.010

mediainfo 命令

0   Pictures $ mediainfo couple.png
General
Complete name                            : couple.png
Format                                   : PNG
Format/Info                              : Portable Network Graphic
File size                                : 1.37 MiB

Image
Format                                   : LZ77
Width                                    : 1 440 pixels
Height                                   : 900 pixels
Bit depth                                : 24 bits

mediainfo 还可适用于其他多媒体文件,如音频、视频等

0   Music $ mediainfo 时间都去哪儿了.mp3 
General
Complete name                            : 时间都去哪儿了.mp3
Format                                   : MPEG Audio
File size                                : 3.39 MiB
Duration                                 : 3mn 40s
Overall bit rate mode                    : Constant
Overall bit rate                         : 128 Kbps
Album                                    : 时间都去哪儿了
Track name                               : 时间都去哪儿了
Performer                                : 王铮亮
Recorded date                            : 2013-12-20
Writing library                          : LAME3.99.5
Cover                                    : Yes
Cover description                        : e
Cover type                               : Cover (front)
Cover MIME                               : image/jpeg

Audio
Format                                   : MPEG Audio
Format version                           : Version 1
Format profile                           : Layer 3
Mode                                     : Joint stereo
Duration                                 : 3mn 40s
Bit rate mode                            : Constant
Bit rate                                 : 128 Kbps
Channel(s)                               : 2 channels
Sampling rate                            : 44.1 KHz
Compression mode                         : Lossy
Stream size                              : 3.36 MiB (99%)
Writing library                          : LAME3.99.5

feh 命令

0   Pictures $ feh -l smoke.jpg couple.png 
NUM FORMAT  WIDTH   HEIGHT  PIXELS  SIZE(bytes) ALPHA   FILENAME
1   jpeg    1920    1200    2304000 328285      -   smoke.jpg
2   png     1440    900     1296000 1439801     -   couple.png

exiftool 命令

0   Pictures $ exiftool couple.png 
ExifTool Version Number         : 8.60
File Name                       : couple.png
Directory                       : .
File Size                       : 1406 kB
File Modification Date/Time     : 2014:03:16 13:34:01+08:00
File Permissions                : rw-rw-r--
File Type                       : PNG
MIME Type                       : image/png
Image Width                     : 1440
Image Height                    : 900
Bit Depth                       : 8
Color Type                      : RGB
Compression                     : Deflate/Inflate
Filter                          : Adaptive
Interlace                       : Noninterlaced
Background Color                : 255 255 255
Pixels Per Unit X               : 72
Pixels Per Unit Y               : 72
Pixel Units                     : Unknown
Comment                         : ÿþ
Datecreate                      : 2014-03-16T13:31:20+08:00
Datemodify                      : 2014-03-16T13:31:04+08:00
Jpegcolorspace                  : 2
Jpegsampling-factor             : 2x2,1x1,1x1
Image Size                      : 1440x900

附:以上命令大多需要安装独立的软件包

点击这里,查看原论坛讨论。