ImageMagick's convert (in IMv7, magick) handles image conversion and editing from a script or shell.
Resize¶
convert in.png -resize 50% out.png # scale by percentage, preserving aspect ratio
convert in.png -resize 800x600 out.png # fit within a box, preserving aspect ratio
convert in.png -resize 800x600! out.png # force exact dimensions (the ! ignores aspect ratio)
convert in.png -scale 500% out.png # enlarge (nearest/simple scaling, faster, lower quality than resize)
If a -resize to an exact size doesn't quite land on the requested dimensions (ImageMagick preserves aspect ratio by default), check with identify and add ! to force it, accepting some distortion.
File size and quality¶
cp photo.jpg photo.jpg.bak
convert photo.jpg -quality 75 photo.jpg # re-encode a JPEG at 75% quality to shrink it
convert photo.jpg -resize 2000x2000\> -quality 80 photo.jpg # only shrink if larger than 2000px (the \> guards against upscaling)
Always keep a backup before overwriting the original in place, since re-compression is lossy.
Inspect¶
identify photo.jpg
# photo.jpg JPEG 3872x2592 3872x2592+0+0 8-bit DirectClass 756KiB 0.000u 0:00.009
identify -verbose photo.jpg | less # full metadata (EXIF, colour profile, etc.)
Screenshots from the shell (X11)¶
import -window root screenshot.png # ImageMagick's own tool (X11 only; not for Wayland)
On Wayland desktops use grim or the desktop environment's own screenshot tool instead; ImageMagick's import does not see Wayland surfaces.
Batch operations¶
for f in *.png; do convert "$f" -resize 50% "resized/$f"; done
mogrify -resize 800x600 -format jpg *.png # in-place batch conversion (mogrify modifies/overwrites)
mogrify writes over its inputs unless -format/-path sends output elsewhere. Test on copies first.