Command Line Image Editing

Nov. 3, 2023 [technology] [guides] [libre]

Some more lines in the streak of command line $THING editing, I’d like to now cover handling images. And here we will naturally lean into the excellent imagemagick. Even though the documentation suggests that imagemagick can be invoked with magick, this does not appear to be the case across distributions. At least not mine, which rather breaks imagemagick up into individual invocations.

Resize an image

Supplying the change as a percentage is the fastest way to maintain aspect ratio.

mogrify -resize 60% sample.png

Be forewarned that mogrify modifies files in place (which is desirable IMO). Alternatively, an exact resultion can be specified:

mogrify -resize 640x360 sample.png

Flip an image

Images can be reversed with flip/flop arguments. To flip horizontally:

convert -flop sample.jpg
Opossum. Opossum, but turned around.

Or flip it vertically (while keeping the original file untouched):

convert sample.jpg -flip sample_upside_down.jpg

Crop an image

Remove the edges from images by pixel depth:

convert sample.png -gravity South -chop 0x60 shorter.png

Directional North/S/E/W can be supplied to select the edge to operate over. The boundaries can also be defined as a rectangle:

convert sample.png -gravity center -crop 640x360+0+0 cropped.png

The +0+0 represent the offsets in X and Y directions.

Overlay images

If you’re working with transparency, it may be desirable to superimpose one image over another.

composite -gravity center foreground.png background.png composite.png

Will create an output named composite.png. Note that the last file supplied before the output will be furthest into the background.

Convert between image formats

Super simple and definitely beats opening a graphical editor just to save it as another image format.

convert sample.tiff sample.jpg

Could easily be scripted to iterate over directories and make files consistent, if one desired to do so.

Many possibilities exist, among them applying text and blur as well as other filters. imagemagick is one of the core components relied on by my yt-linkifier tool to create consistently sized thumbnail previews. I consider imagemagick to be the “ffmpeg” of image files and it similarly has saved me a lot of time.