美文网首页
sips命令行图片处理工具

sips命令行图片处理工具

作者: reviewThis | 来源:发表于2021-07-27 15:37 被阅读0次

概述:

sips是一个命令行的图片处理工具,具有转换图片格式、修改图片大小(扩充或者重新采样缩小图片)、修改质量,设置版权信息等功能

Tips: 使用sips的时候要注意,如果不用--out指定输出目录或者图片名,sips会直接修改原始图片。

查看sips是否存在

which sips
/usr/bin/sips

用法

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 支持可修改图片属性

图片的属性分为Image property和Profile property,每一类都有十多个配置项。 sips能够查阅修改图片的属性,用--getProperty查看,用--setProperty修改,用--deleteProperty删除。

     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 --getProperty all swift-log.png
/Users/username/Documents/swift-log.png
  pixelWidth: 120
  pixelHeight: 120
  typeIdentifier: public.png
  format: png
  formatOptions: default
  dpiWidth: 72.000
  dpiHeight: 72.000
  samplesPerPixel: 4
  bitsPerSample: 8
  hasAlpha: yes
  space: RGB
  profile: sRGB IEC61966-2.1

转换格式
-s format指定目标格式,-s formatOptions指定图片质量,图片质量支持default、low、normal、high、best、 百分比、lzw、packbits:

 sips -s format jpeg -s formatOptions default swift-log.png --out swift-log.jpeg

修改图片大小

查看图片大小
file swift-log.png 
swift-log.png: PNG image data, 120 x 120, 8-bit/color RGBA, non-interlaced

更改图片大小
sips --padToHeightWidth 256 256 swift-log.png --out swift-log-256.png

查看图片大小
file swift-log-256.png   
swift-log-256.png: PNG image data, 256 x 256, 8-bit/color RGBA, non-interlaced

裁剪操作不会对图片进行缩放,如果指定的裁剪尺寸大于原始图片,多处的区块被填充,如果小于原始图片,多余的区块被丢弃。

sips --cropToHeightWidth 256 256 --padColor FFFFFF swift-log.png  --out swift-log-256.png 

用采样的方式修改图片大小,不会产生填空区块,或者将多余区域丢弃,如果长宽比例和原始图片不同,图片会变形(拉长或压缩)。

sips --resampleHeightWidth 256 256  swift-log.png --out swift-log-256.png 

可以用任意角度旋转图片,度数为正,是顺时针旋转,旋转时,图片的长宽会被改变。如果不是90度的倍数,会产生填充。

sips --rotate 45  --padColor FFFFFF swift-log.png --out rot-45-swift.png

图片反转

sips --flip horizontal swift-log.png --out h-swift.png  # 水平翻转
sips --flip vertical  swift-log.png --out v-swift.png # 垂直翻转

相关文章

网友评论

      本文标题:sips命令行图片处理工具

      本文链接:https://www.haomeiwen.com/subject/orromltx.html