Basic photo editing recipes for the command line

2022-09-06

When I started using Linux systems I was initially attracted to the free cost, its lineage of open source software, and the challenge and adventure of using new tools. I still like those things. But more and more I've come to realize that those feel closer to personal preferences than clear advantages. Sure, free is nice, but open source software is often maligned as kludgy - awkward interfaces, for example, or tools that don't quite work without fine tuning your system settings. To be fair, that does occasionally trip me up.

But one of the huge advantages of Linux that I've learned over time is the composability of commands in the command line. With a few terse words you can coax out powerful tools rather than the amount of time searching in GUI software. And more importantly, you can by its very CLI nature automate repetitive tasks, even complex ones.

For years now I've done the majority of my photo prepping and editing with ImageMagick in the command line.

Use ImageMagick® to create, edit, compose, or convert digital images. It can read and write images in a variety of formats (over 200) including PNG, JPEG, GIF, WebP, HEIC, SVG, PDF, DPX, EXR and TIFF. ImageMagick can resize, flip, mirror, rotate, distort, shear and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses and Bézier curves.

What follows are some of the recipes I use for individual and mass editing photos.

Basic image conversion

convert input.jpg output.png

Convert and resize

convert input.jpg -resize 30% output.png

Convert bulk images without making new files

This will take all images in the folder, resize them to 800x600, then convert them to png files

mogrify convert -resize 800x600 -format jpg *.png

Dither

ImageMagick logo dithered
ImageMagick logo dithered

There are many dithering options available but here's a simple one I tend to use - it dithers and converts to grayscale.

convert input.jpg -resize 800x600 -colorspace gray -ordered-dither 8x8 output.gif

Remove white background, convert images to png files

Bash script that loops through all png files in a folder, removing the white so there is a transparent background, and re-numbering all image files.

cnt=1
for file in *.png
do
    convert $file -fuzz 20% -transparent white ${cnt}.png
    cnt=$(( $cnt + 1 ))
done

Links

Example ImageMagick usage

Fred's ImageMagick Scripts give hundreds of examples. Check out the lomography-effect script.

Back to index

---

Leave a public comment by emailing lettuce at the current domain and putting comment in your subject. Please let me know what name you'd like to be listed.