Skip to main content

imagemagick

tip
  • 新版本 convert, magick convert 命令被弃用,使用 magick 替代
# 添加 svg 和 opencl 支持
brew reinstall imagemagick

# 处理 SVG 也可以考虑 inkscape
# 但在 macos 下 xserver 显示的不太好
# brew cask info inkscape
#
magick hd.jpg -resize 1920x1080 hd_1080p.png
magick hd.jpg -resize 2048x1080 hd_2k.png
magick hd.jpg -resize 2048x hd_w2048.png

magick hd.jpg -resize 1080x hd_w1080.png

magick hd.jpg -fuzz 5% -trim +repage -alpha set -bordercolor none -border 48x48 -resize 1080x hd_w1080.png

# 照片保持原本的方向
magick hd.jpg -auto-orient -resize 1080x hd_w1080.png
# 正方形
magic input.jpg -auto-orient -gravity center --corp 1:1 output.png
  • 裁剪
    • 显著性 Saliency
    • 熵 Entropy

参数顺序

资源 -> 输入选项 -> 输入文件 -> 输出选项 -> 输出文件
commandfor
mogrify批量转换, 原地修改
convert转换格式

Convert​

# -append 自上至下合并
# +append 自左至右合并
convert a.png b.png -append full.png

convert -resize ${resize}x${resize} -unsharp 1x4

mogrify -resize 16x12 -quality 100 -path ../new-thumbs *.jpg

# 合并 PDF
convert -density 150 *.pdf output.pdf
convert -density 600 file1.pdf file2.pdf -resize 50% new.pdf

# ghostscript
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=temp.pdf pdf1.pdf pdf2.pdf
# PDF Toolkit
pdftk pdf1.pdf pdf2.pdf cat output temp.pdf

magick mogrify -monitor -format jpg *.HEIC

Options​

  • -limit type value
    • width, height, area, memory, map, disk, file, thread, throttle, time
    • 默认 768 files, 3GB of image area, 1.5GiB memory, 3GiB memory map, and 18.45EB of disk
    • 当前限制 identify -list resource
    • memory - 限制内存上限
    • map - 限制内存映射上限
    • disk - 限制磁盘使用上限
    • area - 限制图像区域上限 - 超过这个限制的图像将被直接放到磁盘,避免一张图片就占用过多内存
    • thread - 限制线程数
  • https://imagemagick.org/script/command-line-options.php

FAQ

The convert command is deprecated in IMv7, use "magick" instead of "convert" or "magick convert"​

  • convert, magick convert -> magick
  • IM7

  • convert, mogrify, identify, composite -> magick

Nonstandard tile width 2556, convert file.​

  • TIFF 的 tile width 和 tile length 不是 16 的倍数

Unknown field with tag ... encountered​

Unknown field with tag 512 (0x200) encountered.
Unknown field with tag 513 (0x201) encountered.
Unknown field with tag 514 (0x202) encountered.
Unknown field with tag 515 (0x203) encountered.
  • TIFFReadDirectory
tagfor
512JPEGProc
513JPEGInterchangeFormat
514JPEGInterchangeFormatLength
515JPEGRestartInterval
identify -verbose file.tif
exiftool file.tif