Skip to content

Instantly share code, notes, and snippets.

@dlebech
Created May 2, 2021 20:05
Show Gist options
  • Save dlebech/63d305a9bb7ca530faf20fc1629cd756 to your computer and use it in GitHub Desktop.
Save dlebech/63d305a9bb7ca530faf20fc1629cd756 to your computer and use it in GitHub Desktop.
Some useful one-liners for Imagemagick, including resizing, convert to pencil and fake miniature
# Make * work in bash or zsh:
# zsh
setopt extendedglob
# bash
shopt -s extglob
# Resize all images (in-place) in a folder
# to 1200 pixels on the longest side
mogrify -resize 1200 *.jpg
# Resize and reduce quality to be better suited for a blog post.
# Usually, I don't notice a visible drop in quality,
# but I'm also not an expert :-)
convert -format jpg \
-strip \
-interlace plane \
-quality 50% photo.jpg \
-resize 1200 photo_resized.jpg
# Find the edges/outlines of a photo, not too many lines
# (canny is the name for an algorithm that finds outlines)
convert ^*canny*.jpg \
-set filename:original %t \
-resize 1200 \
-canny 0x3+5%+15% \
-negate '%[filename:original]_canny1.jpg'
# Find the edges/outlines of a photo, many lines
convert ^*canny*.jpg \
-set filename:original %t \
-resize 1200 \
-canny 0x1+10%+20% \
-negate '%[filename:original]_canny2.jpg'
# -Sketch parameter outputs pencil-like drawing. It works very differently on
# different images. You might want to play around with removing the -negate
# parameter. It worked well for a fairly bright photo but might not for others.
# See also: https://legacy.imagemagick.org/Usage/photos/
convert photo.jpg \
-resize 1200 \
-colorspace gray \
-negate \
-sketch 0x20+120 photo_sketch.jpg
# A slightly better (at least for me it seems) version of the above is to
# create a pencil tile with motion blur and then apply it over the image
# See also: https://legacy.imagemagick.org/Usage/photos/
convert -size 256x256 \
xc: +noise Random \
-virtual-pixel tile \
-motion-blur 0x20+20 \
-charcoal 1 \
-resize 50% pencil_tile.gif
# And then
convert photo.jpg \
-colorspace gray \
\( +clone -tile pencil_tile.gif -draw "color 0,0 reset" \
+clone +swap -compose color_dodge -composite \) \
-fx 'u*.2+v*.8' photo_sketch2.jpg
# There's also charcoal of course.
# See also: https://legacy.imagemagick.org/Usage/photos/
convert photo.jpg \
-charcoal 1 photo_char.jpg
# Make a photo look like a miniature
convert photo.jpg -sigmoidal-contrast 10x50% \
\( +clone -sparse-color Barycentric '0,0 black 0,%h gray80' \
-solarize 50% -level 30%,0 -write mpr:blur_map \) \
-compose Blur -set option:compose:args 10x0 \
-composite mpr:blur_map \
-compose Blur \
-set option:compose:args 0x10 \
-composite photo_miniature.jpg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment