简述

Macos下,查看图片,默认就是使用Preview。Preview基本包含了普通人能用到的各种功能。
但有时候需要批量处理图片时,常规的GUI工具会显得不太方便,使用命令来操作能够更快捷高效。下面介绍几个Macos下和图片相关的几个命令。

查看图片尺寸

系统自带file命令

在命令行下查看图片的分辨率,可以直接使用Macos自带的file命令即可。

1
2
3
4
5
6
7
8
$ ls
Diamond.png kulipa.jpg steve.jpeg
$
$ file *
Diamond.png: PNG image data, 256 x 256, 4-bit colormap, non-interlaced
kulipa.jpg: JPEG image data, JFIF standard 1.01, resolution (DPI), density 1x1, segment length 16, baseline, precision 8, 220x220, frames 3
steve.jpeg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 220x220, frames 3
$

其中256 x 256就是Diamond.png的分辨率,220x220是kulipa.jpg和steve.jpeg的分辨率。

ImageMagick中的identify

如果还需要图片的更多内容,那么可以使用identify命令。identify是强大的开源图片处理工具ImageMagick的一个组件。

Macos下可以通过使用Homebrew来安装ImageMagick: brew install imagemagick

安装完毕后,就可以使用identify命令了。 简单的用法就是 identify ${image_name}

1
2
3
4
5
6
7
8
$ ls
Diamond.png kulipa.jpg steve.jpeg
$
$ identify *
Diamond.png PNG 256x256 256x256+0+0 8-bit sRGB 499B 0.000u 0:00.000
kulipa.jpg[1] JPEG 220x220 220x220+0+0 8-bit sRGB 10.9KB 0.000u 0:00.000
steve.jpeg[2] JPEG 220x220 220x220+0+0 8-bit sRGB 9.62KB 0.000u 0:00.000
$

就能够显示分辨率,图片大小等基本信息了。

也可以使用-format参数来输出指定的信息。-format的具体参数参照ImageMagick官网说明

1
2
3
4
5
6
7
8
9
$ ls
Diamond.png kulipa.jpg steve.jpeg
$
# 下面-format参数中,%f代表filename,%w代表width in pixels,%h代表height in pixels,%m表示文件格式
$ identify -format "%f: %wx%h file_format:%m\n" *
Diamond.png: 256x256 file_format:PNG
kulipa.jpg: 220x220 file_format:JPEG
steve.jpeg: 220x220 file_format:JPEG
$

也可以使用-verbose参数来打印图片的全部信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
$ identify -verbose steve.jpeg
Image: steve.jpeg
Format: JPEG (Joint Photographic Experts Group JFIF format)
Mime type: image/jpeg
Class: DirectClass
Geometry: 220x220+0+0
Units: Undefined
Type: TrueColor
Endianess: Undefined
Colorspace: sRGB
Depth: 8-bit
Channel depth:
red: 8-bit
green: 8-bit
blue: 8-bit
Channel statistics:
Red:
min: 0 (0)
max: 255 (1)
mean: 102.506 (0.401985)
standard deviation: 95.4329 (0.374247)
kurtosis: -1.49406
skewness: 0.402081
Green:
min: 0 (0)
max: 255 (1)
mean: 154.898 (0.607445)
standard deviation: 55.8926 (0.219187)
kurtosis: -0.0319889
skewness: -0.290039
Blue:
min: 0 (0)
max: 255 (1)
mean: 127.662 (0.500634)
standard deviation: 71.2261 (0.279318)
kurtosis: -1.0386
skewness: 0.188506
Image statistics:
Overall:
min: 0 (0)
max: 255 (1)
mean: 128.355 (0.503355)
standard deviation: 75.9486 (0.297838)
kurtosis: -0.862748
skewness: -0.0758529
Rendering intent: Perceptual
Gamma: 0.454545
Chromaticity:
red primary: (0.64,0.33)
green primary: (0.3,0.6)
blue primary: (0.15,0.06)
white point: (0.3127,0.329)
Background color: white
Border color: srgb(223,223,223)
Matte color: grey74
Transparent color: black
Interlace: None
Intensity: Undefined
Compose: Over
Page geometry: 220x220+0+0
Dispose: Undefined
Iterations: 0
Compression: JPEG
Quality: 75
Orientation: Undefined
Properties:
date:create: 2018-04-25T19:46:03+08:00
date:modify: 2016-09-13T09:57:32+08:00
jpeg:colorspace: 2
jpeg:sampling-factor: 2x2,1x1,1x1
signature: 6d01e3f2f45a94c2ec30e4928fdb3fbbc82c7d5e556e71a9a981868f6cdbc778
Artifacts:
filename: steve.jpeg
verbose: true
Tainted: False
Filesize: 9.62KB
Number pixels: 48.4K
Pixels per second: 4.84MB
User time: 0.000u
Elapsed time: 0:01.009
Version: ImageMagick 6.8.8-6 Q16 x86_64 2014-02-17 http://www.imagemagick.org
$

命令行中打开图片

Macos自带的open命令

Macos自带了强大的open命令,open后面加上图片路径,就会使用系统的preview来打开这个图片。

1
2
3
4
5
$ ls
Diamond.png kulipa.jpg steve.jpeg
$
$ open *
$

输入open *指令后,就会使用preview来打开Diamond.png,kulipa.jpg和steve.jpeg这三张图片。效果和在Finder中选中这三张图片,然后选择使用Preview打开效果是一样的。
open_image_on_terminal.jpg

open是Macos的一个强大的命令,能做很多事情,比如

  • 使用Finder打开当前目录 open ./
  • 使用Chrome打开链接 open "https://www.google.com" -a Google\ Chrome

直接在Terminal中查看图片

有一些工具,安装配置后能直接在Terminal中展示图片。目前没有这方面的需求,没有试过。摘录几个网址,有空了可以试了玩玩。

命令行转换图片格式

系统自带的sips命令

Macos自带了一个命令可以用来转换图片格式,名字叫做sips

转换格式的用法如下:

1
sips -s format [转换的目标格式] --out [目标文件名字] [输入文件]

如下是一个将jpg图片转换为png图片的例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ ls
kulipa.jpg
# 将kulipa.jpg转换为png格式,输出名字叫kulipa.png
$ sips -s format png --out kulipa.png kulipa.jpg
/Users/carlshen/tmp/command_line_image_check/kulipa.jpg
/Users/carlshen/tmp/command_line_image_check/kulipa.png
$
$ ls
kulipa.jpg kulipa.png
$
$ file kulipa.jpg kulipa.png
kulipa.jpg: JPEG image data, JFIF standard 1.01, resolution (DPI), density 1x1, segment length 16, baseline, precision 8, 220x220, frames 3
kulipa.png: PNG image data, 220 x 220, 8-bit/color RGB, non-interlaced
$

支持的格式有jpeg | tiff | png | gif | jp2 | pict | bmp | qtif | psd | sgi | tga
不同系统的sips支持的格式可能不同,可以使用man sips来查看sips支持的转换格式

sips除了能转换图片格式以外,还可以对图片进行调整大小(resize),旋转(rotate)和翻转(flip)等。

  • 拿一个原size是1200x896的house.jpg为例:
    • 限定范围缩放命令: sips -Z pixelsWH [file]
      • 例子: sips -Z 300 house.jpg
      • 将原图缩放到300x300像素的方框内,保持图片的长宽比不变
    • 限定宽度缩放命令: sips --resampleWidth pixelsW [file]
      • 例子: sips --resampleWidth 300 house.jpg
      • 将原图缩放到宽度是300,长度等比例缩放,保持图片的长宽比不变
    • 限定高度缩放命令: sips --resampleHeight pixelsH [file]
      • 例子: sips --resampleHeight 300 house.jpg
      • 将原图缩放到长度是300,宽度等比例缩放,保持图片的长宽比不变
    • 调整大小命令: sips -z height width [file]
      • 例子: sips -z 400 400 house.jpg
      • 图片会被缩小拉伸到400x400,原图片的长宽比会改变
    • 旋转图片命令: sips -r degreesCW [file]
      • 例子: sips -r 90 house.jpg
      • 图片会顺时针旋转90度
    • 翻转图片命令: sips -f horizontal|vertical [file]
      • 例子: sips -f horizontal house.jpg
      • 图片会水平翻转。如果使用vertical, 则会垂直翻转
    • 注意,上述命令会直接修改原图片,如果要保留原图片,则可以加上–out 参数指定输出的文件名。比如sips -f horizontal house.jpg --out house_horizontal.jpg

使用convert命令

convert也是ImageMagick的组件之一。简单的转换格式用法非常的简单

1
convert [源文件] [目标文件]

convert会根据文件后缀自动转换为想要的格式

1
2
3
4
5
6
7
8
9
10
11
12
13
$
$ convert kulipa.jpg kulipa.png # 转换为png文件
$ convert kulipa.jpg kulipa.bmp # 转换为bmp文件
$
$ ls
kulipa.bmp kulipa.jpg kulipa.png
$
# 检查各个图片的格式
$ identify *
kulipa.bmp BMP 220x220 220x220+0+0 8-bit sRGB 145KB 0.000u 0:00.000
kulipa.jpg[1] JPEG 220x220 220x220+0+0 8-bit sRGB 10.9KB 0.000u 0:00.009
kulipa.png[2] PNG 220x220 220x220+0+0 8-bit sRGB 15.1KB 0.000u 0:00.000
$

Reference

留言