写在前面
最近有需求转换图片的格式, 例如
- 将
.PDF
转换为.png
以便CSDN编辑器上传(如果直接使用截图会显得比较麻烦,多费一道手续), - 或是将微信的一些图片制作成表情包(需要转换图片为
.gif
格式),
这些操作看似简单, 但是我一直没有找到一个方便快捷的操作.
对第一个要求, 一开始我在网上搜索[^6], 提到使用
TeX
L
i
v
e
\TeX\rm Live
TEXLive自带的gs
命令进行PDF到图片格式的转换, 但是命令较为复杂, 如下
gs -sDEVICE=png16m -sOutputFile=newfig.png -dNOPAUSE -r1200 old.pdf quit.ps
gs -sDEVICE=png16m -sBATCH -sOutputFile=newfig.png -dNOPAUSE -r1200 old.pdf
经尝试这两个命令都可以进行PDF到图片的转换, 但是还是略显复杂了.
之后我在1评论中看到了有人用convert
命令(实际上是ImageMagick
的magick
命令)和sips
命令进行图片格式转换, 顿时让我觉得眼前一亮, 并且鉴于网上对于这个命令的介绍较少, 于是写此文进行总结.
convert
命令(magick
)
convert
命令其实是imagemagick
程序包的一个命令, 通过ls -al
可以方便的看到软链接
❯ ls -l /opt/homebrew/Cellar/imagemagick/7.1.0-19/bin/convert
lrwxr-xr-x 1 hep staff 6 12 23 05:05 /opt/homebrew/Cellar/imagemagick/7.1.0-19/bin/convert -> magick
- 官网给出的教程也说
convert
其实等价于magick
的.
这个软件包是安装MacTeX时候的附带产品, 如果大家没安装的话可以使用brew install imagemagick
直接安装, 开源强大的图片处理转换命令行工具. 其功能不亚于下面提到的sips
, 有兴趣的话大家可以到其官方主页2进行深入学习了解.
执行转换命令只需要传入待转换的图片以及输出的图片文件名即可, 还是很方便的
convert aa.eps a.ico
简直不要再方便, 而且支持几乎所有的位图/矢量图格式. 美中不足的一点就是转换的图片质量不高,
sips
(scriptable image processing system)
使用macOS独有的脚本化图片处理系统sips
命令, 就可以方便快捷进行图片的格式转换, 而且还支持图片的裁剪旋转等各种常用操作, 只需要熟悉基本的bash命令就可以实现, 还是很值得学习的. 所以这里也着重介绍一下sips
命令.
官方文档
首先来看其官方文档(使用sips --help
命令也可以查看)
❯ sips --help
sips - scriptable image processing system.
This tool is used to query or modify raster image files and ColorSync ICC profiles.
Its functionality can also be used through the "Image Events" AppleScript suite.
Usages:
sips [image-functions] imagefile ...
sips [profile-functions] profile ...
Profile query functions:
-g, --getProperty key
-X, --extractTag tag tagFile
--verify
-1, --oneLine
Image query functions:
-g, --getProperty key
-x, --extractProfile profile
-1, --oneLine
Profile modification functions:
-s, --setProperty key value
-d, --deleteProperty key
--deleteTag tag
--copyTag srcTag dstTag
--loadTag tag tagFile
--repair
-o, --out file-or-directory
Image modification functions:
-s, --setProperty key value
-d, --deleteProperty key
-e, --embedProfile profile
-E, --embedProfileIfNone profile
-m, --matchTo profile
-M, --matchToWithIntent profile intent
--deleteColorManagementProperties
-r, --rotate degreesCW
-f, --flip horizontal|vertical
-c, --cropToHeightWidth pixelsH pixelsW
--cropOffset offsetY offsetH
-p, --padToHeightWidth pixelsH pixelsW
--padColor hexcolor
-z, --resampleHeightWidth pixelsH pixelsW
--resampleWidth pixelsW
--resampleHeight pixelsH
-Z, --resampleHeightWidthMax pixelsWH
-i, --addIcon
--optimizeColorForSharing
-o, --out file-or-directory
-j, --js file
Other functions:
--debug Enable debugging output
-h, --help Show help
-H, --helpProperties Show help for properties
--man Generate man pages
-v, --version Show the version
--formats Show the read/write formats
由于上面的一些命令参数需要制定具体的图片属性, 下面列出了官方文档中的所有键值参数(sips --helpProperties
)
❯ sips --helpProperties
sips - scriptable image processing system.
This tool is used to query or modify raster image files and ColorSync ICC profiles.
Its functionality can also be used through the "Image Events" AppleScript suite.
The commands --getProperty, --setProperty, and --deleteProperty
can use one of the following keys as a parameter.
Special property keys:
all binary data
allxml binary data
Image property keys:
dpiHeight float
dpiWidth float
pixelHeight integer (read-only)
pixelWidth integer (read-only)
typeIdentifier string (read-only)
format string jpeg | tiff | png | gif | jp2 | pict | bmp | qtif | psd | sgi | tga
formatOptions string default | [low|normal|high|best|<percent>] | [lzw|packbits]
space string (read-only)
samplesPerPixel integer (read-only)
bitsPerSample integer (read-only)
creation string (read-only)
make string
model string
software string (read-only)
description string
copyright string
artist string
profile binary data
hasAlpha boolean (read-only)
Profile property keys:
description utf8 string
size integer (read-only)
cmm string
version string
class string (read-only)
space string (read-only)
pcs string (read-only)
creation string
platform string
quality string normal | draft | best
deviceManufacturer string
deviceModel integer
deviceAttributes0 integer
deviceAttributes1 integer
renderingIntent string perceptual | relative | saturation | absolute
creator string
copyright string
md5 string (read-only)
这里列出可以进行转换的格式sips --formats
❯ sips --formats
Supported Formats:
-------------------------------------------
com.adobe.pdf pdf Writable
com.adobe.photoshop-image psd Writable
com.adobe.raw-image dng
com.apple.atx -- Writable
com.apple.icns icns Writable
com.apple.pict pict
com.canon.cr2-raw-image cr2
com.canon.cr3-raw-image cr3
com.canon.crw-raw-image crw
com.canon.tif-raw-image --
com.compuserve.gif gif Writable
com.dxo.raw-image dxo
com.epson.raw-image erf
com.fuji.raw-image raf
com.hasselblad.3fr-raw-image 3fr
com.hasselblad.fff-raw-image fff
com.ilm.openexr-image exr Writable
com.kodak.raw-image dcr
com.konicaminolta.raw-image mrw
com.leafamerica.raw-image mos
com.leica.raw-image raw
com.leica.rwl-raw-image rwl
com.microsoft.bmp bmp Writable
com.microsoft.cur --
com.microsoft.dds dds Writable
com.microsoft.ico ico Writable
com.nikon.nrw-raw-image nrw
com.nikon.raw-image nef
com.olympus.or-raw-image orf
com.olympus.raw-image orf
com.olympus.sr-raw-image orf
com.panasonic.raw-image raw
com.panasonic.rw2-raw-image rw2
com.pentax.raw-image pef
com.phaseone.raw-image iiq
com.samsung.raw-image srw
com.sgi.sgi-image sgi
com.sony.arw-raw-image arw
com.sony.raw-image srf
com.sony.sr2-raw-image sr2
com.truevision.tga-image tga Writable
org.khronos.astc astc Writable
org.khronos.ktx ktx Writable
org.webmproject.webp webp
public.avci avci
public.heic heic Writable
public.heics heics Writable
public.heif heif
public.jpeg jpeg Writable
public.jpeg-2000 jp2 Writable
public.mpo-image mpo
public.pbm pbm Writable
public.png png Writable
public.pvr pvr Writable
public.radiance pic
public.tiff tiff Writable
主要使用场景
转换图片格式
这里列出几个使用sips
命令的具体例子
-
pdf->png
sips -s format png old.pdf -o new.png
-
pdf->jpg
sips -s format jpeg old.pdf -o new.jpg
-
jpg->gif
sips -s format gif old.jpg -o new.gif
其他常见格式如tiff
, bmp
, psd
等也可以进行相互转换, 命令格式如上. 但是,美中不足的是ico
格式(图标)不能通过sips
实现, 只能通过imagemagick来操作.
修改图片为指定像素
-
修改图片为20000像素宽, 高度为自适应.
sips -Z 20000 a.jpg
-
修改图片为200*200像素
sips -z 200 200 a.jpg
旋转/翻转图片
-
顺时针旋转图片180°
sips -r 180 a.jpg
-
水平/垂直翻转图片
sips -f horizontal a.jpg sips -f vertical a.jpg
几个命令的对比
这里我亲自尝试了各个命令, 并进行简单的对比, 如下表所示, 事实证明, sips
在图片的各项操作中都以其简洁的语法和高效的操作优于其他命令, 但是对于一些需要深度定制图片属性的场景, 还是要辅助于gs
,imagemagick
等命令行工具, 或者直接利用可视化的gimp
, Photoshop
进行处理.
命令 | 命令简介 | 主要使用场景 | 转换PDF与输出质量 | 命令复杂程度 |
---|---|---|---|---|
gs | ghostscript 命令 | 专门用于处理.ps 文件以及PDF 相关的文件 | 直接转换, 质量高 | 高 |
magick (convert ) | imagemagick 工具的命令,需要额外下载安装 | 图片处理 | 质量低,pdf转png会丢失背景 | 低 |
sips | MacOS自带的图片处理命令, 功能强大 | 图片处理, 不能处理ico文件 | 质量高,pdf转png会丢失背景 | 低 |
综上, 我还是推荐使用sips
的, 需要一些额外功能的话可以借助其他命令.