Batch process images with ImageMagick: resize, crop, format conversion, compression, and montage.
Use Cases
- The user says "resize this image to 800px wide" or "batch compress the images in this folder"
- The user says "convert PNG to JPG" or "add a watermark to the image"
- The user says "combine these images into one"
Prerequisites
# macOS
brew install imagemagick
# Windows
winget install ImageMagick.ImageMagick
# Linux
sudo apt install imagemagick
Execution Method
Resize
# 按宽度缩放(保持比例)
magick input.jpg -resize 800x output.jpg
# 按百分比
magick input.jpg -resize 50% output.jpg
# 指定尺寸(可能变形)
magick input.jpg -resize 800x600! output.jpg
Format Conversion
magick input.png output.jpg
magick input.jpg output.webp
Batch Processing
# 批量缩放目录下所有 JPG
for f in *.jpg; do magick "$f" -resize 800x "resized_$f"; done
# 批量转格式
for f in *.png; do magick "$f" "${f%.png}.jpg"; done
Compression
# JPEG 质量压缩
magick input.jpg -quality 80 output.jpg
# 去除元数据减小体积
magick input.jpg -strip -quality 85 output.jpg
Montage
# 水平拼接
magick a.jpg b.jpg +append merged.jpg
# 垂直拼接
magick a.jpg b.jpg -append merged.jpg
Watermark
magick input.jpg -gravity SouthEast -fill white -pointsize 24 \
-annotate +10+10 "© 2026" output.jpg
Output Specifications
- Display the original image dimensions and size before processing
- Display the new dimensions and size after processing, as well as the space saved
- Confirm the number of files and target parameters before batch operations
- Do not overwrite the original files; output to new files or a subdirectory