macOS批量图片处理

800 阅读4分钟

使用命令行工具sips

  • 将目录下所有 jpg 按宽度 30000 放大,输出到指定目录
for i in *.jpg; do sips -Z 30000 "${i}" --out ~/Downloads/img"${i}"; done
  • 将目录下所有 jpg 转换成 png, 输出质量 100
for i in *.jpg;do sips -s format png "${i}" -s formatOptions 100 --out ~/Downloads/img/"${i%jpg}png"; done

sips 说明

机翻

名称
    sips-可编写脚本的图像处理系统。

概要
    sips [image-functions] imagefile ...
    sips [profile-functions]配置文件...

描述
    此工具用于查询或修改光栅图像文件和ColorSync ICC
    配置文件。也可以通过“Image Events”使用其功能
    AppleScript套件。它还支持将JavaScript执行为
    修饰或生成图像。

功能
     配置文件查询功能:

     -g key
     --getProperty key
	   将key的属性值输出到stdout。

     -X tag tagFile
     --extractTag tag tagFile
	   将配置文件标记元素写入tagFile。

     --verify
	   验证任何概要文件问题并将日志输出到stdout。

     -1
     --oneLine
	   使用制表符分隔符在一行上为每个文件输出

     Image query functions:

     -g key
     --getProperty key
	   将key的属性值输出到stdout。

     -x profile
     --extractProfile profile
	   从图像获取嵌入式配置文件并将其写入配置文件。

     -1
     --oneLine
	   使用制表符分隔符在一行上为每个文件输出

     配置文件修改功能:

     -s key value
     --setProperty key value
	   将键的属性值设置为value。

     -d key
     --deleteProperty key
	   删除键的属性值。

     --deleteTag tag
	   从配置文件中删除标签元素。

     --copyTag srcTag dstTag
	   将配置文件的srcTag元素复制到dstTag。

     --loadTag tag tagFile
	   将配置文件的tag元素设置为tagFile的内容。

     --repair
	   修复所有配置文件问题,并将日志输出到stdout。

     -o file-or-directory
     --out file-or-directory

     图像修改功能:

     -s key value
     --setProperty key value
	   将键的属性值设置为value。

     -d key
     --deleteProperty key
	   删除键的属性值。

     -e profile
     --embedProfile profile
	   在图片中嵌入个人资料。

     -E profile
     --embedProfileIfNone profile
	   仅当图像没有配置文件时,才将配置文件嵌入图像中。

     -m profile
     --matchTo profile
	   使图像与配置文件颜色匹配。

     -M profile intent
     --matchToWithIntent profile intent
	   使用渲染意图将图像颜色匹配到配置文件 感性|相对|饱和度|绝对。

     --deleteColorManagementProperties
	   删除TIFF,PNG和EXIF词典中的颜色管理属性。

     -r degreesCW
     --rotate degreesCW

     -f horizontal|vertical
     --flip horizontal|vertical

     -c pixelsH pixelsW
     --cropToHeightWidth pixelsH pixelsW
	   Crop image to fit specified size.

     --cropOffset offsetY offsetH
	   Crop offset from top left corner.

     -p pixelsH pixelsW
     --padToHeightWidth pixelsH pixelsW
	   Pad image with pixels to fit specified size.

     --padColor hexcolor
	   填充时使用此颜色。白色= FFFFFF,红色= FF0000,默认=黑色= 000000

     -z pixelsH pixelsW
     --resampleHeightWidth pixelsH pixelsW
	   以指定尺寸重新采样图像。图像分布比率可能会改变。

     --resampleWidth pixelsW
	   将图像重新采样到指定的宽度。

     --resampleHeight pixelsH
	   将图像重新采样到指定的高度。

     -Z pixelsWH
     --resampleHeightWidthMax pixelsWH
	   对图像重新采样,以使高度和宽度不超过指定的大小。

     -i
     --addIcon
	   将Finder图标添加到图像文件。

     --optimizeColorForSharing
	   优化色彩以进行共享。

     -o file-or-directory
     --out file-or-directory

     -j file
     --js file
	   执行JavaScript文件

     其他功能:

     --debug
	   启用调试输出

     -h
     --help
	   显示帮助

     -H
     --helpProperties
	   显示属性帮助

     --man
	   生成手册页

     -v
     --version
	   显示版本

     --formats
	   显示读/写格式

JavaScript
     可以创建HTML Canvas对象,并将其用于创建2D绘图上下文。
     用于绘制上下文的命令在其他地方都有详细记录。
     本节将描述sip全局对象和其他有趣的类。

     全局变量(sips)属性
     images
	   作为参数传递的有效图像转换为Image对象数组
     arguments
	   参数作为字符串数组传递给程序
     size  
        建议的输出尺寸。设置裁剪或重采样标志将设置该值。
     longestEdge
	   如果指定,则-Z /-resampleHeightWidthMax选项的值。
	   [默认: 0]
     outputPath
	   输出目录[默认:当前目录]

     图像对象
     name  Name of image
     size  Size of image (pixels)
     properties
	   图片属性
     getProperty(name)
	   返回图像属性的名称(如果有)。
     sizeToFitLongestEdge(length)
	   Return the size that will contain the image with the longest edge
	   set to length. Maintains aspect ratio.

     输出对象
     new Output(context, name[, type])
	   将名称和可选类型(扩展名)的上下文输出到磁盘或UTI)。
     addToQueue()
	   将输出添加到要写入磁盘的队列中。

     Functions
     print(str)
	   输出到标准输出。等效于console.log(str)。

选项
     命令--getProperty,--setProperty和--deleteProperty可以使用以下键之一作为参数。

     特殊属性键:
     all	      binary data
     allxml	      binary data

     图片属性键:
     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)

     配置文件属性键:
     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)