使用macOS自带convert,sips命令快速处理图片及转换图片格式 您所在的位置:网站首页 1128156634_16393509658061n.png 使用macOS自带convert,sips命令快速处理图片及转换图片格式

使用macOS自带convert,sips命令快速处理图片及转换图片格式

2023-11-16 02:37| 来源: 网络整理| 查看: 265

写在前面

最近有需求转换图片的格式, 例如

将.PDF转换为.png以便CSDN编辑器上传(如果直接使用截图会显得比较麻烦,多费一道手续),或是将微信的一些图片制作成表情包(需要转换图片为.gif格式),

这些操作看似简单, 但是我一直没有找到一个方便快捷的操作.

对第一个要求, 一开始我在网上搜索[^6], 提到使用 TeX L i v e \TeX\rm Live TE​XLive自带的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|] | [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与输出质量命令复杂程度gsghostscript命令专门用于处理.ps文件以及PDF相关的文件直接转换, 质量高高magick(convert)imagemagick工具的命令,需要额外下载安装图片处理质量低,pdf转png会丢失背景低sipsMacOS自带的图片处理命令, 功能强大图片处理, 不能处理ico文件质量高,pdf转png会丢失背景低

综上, 我还是推荐使用sips的, 需要一些额外功能的话可以借助其他命令.

主要参考

Command(s) which would render a LaTeX document into a PNG on Mac - TeX - LaTeX Stack Exchange; ↩︎

ImageMagick – Command-line Tools: Magick; ↩︎



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有