天天看點

使用macOS自帶convert,sips指令快速處理圖檔及轉換圖檔格式

寫在前面

最近有需求轉換圖檔的格式, 例如

  1. 将​

    ​.PDF​

    ​​轉換為​

    ​.png​

    ​以便CSDN編輯器上傳(如果直接使用截圖會顯得比較麻煩,多費一道手續),
  2. 或是将微信的一些圖檔制作成表情包(需要轉換圖檔為​

    ​.gif​

    ​格式),

這些操作看似簡單, 但是我一直沒有找到一個友善快捷的操作.

對第一個要求, 一開始我在網上搜尋[^6], 提到使用自帶的​​

​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 ->      
  • 官網給出的教程也說​

    ​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      

由于上面的一些指令參數需要制定具體的圖檔屬性, 下面列出了官方文檔中的所有鍵值參數(​

​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​

​指令的具體例子

  1. ​pdf->png​

sips -s format      
  1. ​pdf->jpg​

sips -s format      
  1. ​jpg->gif​

sips -s format      

其他常見格式如​

​tiff​

​​, ​

​bmp​

​​, ​

​psd​

​​等也可以進行互相轉換, 指令格式如上. 但是,美中不足的是​

​ico​

​​格式(圖示)不能通過​

​sips​

​實作, 隻能通過imagemagick來操作.

修改圖檔為指定像素

  1. 修改圖檔為20000像素寬, 高度為自适應.
sips -Z 20000 a.jpg      
  1. 修改圖檔為200*200像素
sips -z 200 200 a.jpg      

旋轉/翻轉圖檔

  1. 順時針旋轉圖檔180°
sips -r 180 a.jpg      
  1. 水準/垂直翻轉圖檔
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會丢失背景

主要參考

  1. ​​Command(s) which would render a LaTeX document into a PNG on Mac - TeX - LaTeX Stack Exchange​​​; ​​↩︎​​
  2. ​​ImageMagick – Command-line Tools: Magick​​​; ​​↩︎​​

繼續閱讀