Skip to content

Instantly share code, notes, and snippets.

@MichaelPaulukonis
Last active August 31, 2023 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichaelPaulukonis/e1242f0345393d53b18826448e31f748 to your computer and use it in GitHub Desktop.
Save MichaelPaulukonis/e1242f0345393d53b18826448e31f748 to your computer and use it in GitHub Desktop.
ImageMagick + related scripts, one-liners etc.

imagemagick

  • converts webp to png 1 for x in *.webp; do dwebp {} -o ${x%.*}.png ::: $x; done
  • convert transparent pngs to white background (multiple files w/ renaming)
    • convert '*.png' -background white -alpha remove -alpha off -set filename:fn '%[basename]-white' '%[filename:fn].png'

ImageMagick CLI

Separate image into discrete channels, colored as channel

convert CMYK-Chart.png -colorspace cmyk -channel c -negate -separate channel_c.png
convert CMYK-Chart.png -colorspace cmyk -channel m -negate -separate channel_m.png
convert CMYK-Chart.png -colorspace cmyk -channel y -negate -separate channel_y.png
convert CMYK-Chart.png -colorspace cmyk -channel k -negate -separate channel_k.png

convert channel_c.png +level-colors cyan,white channel_c_colored.png
convert channel_m.png +level-colors magenta,white channel_m_colored.png
convert channel_y.png +level-colors yellow,white channel_y_colored.png

IM scripts

slice 'em up (cut/split image into tiles)

  • convert rose: -crop 3x3@ +repage +adjoin rose_3x3@_%d.gif

convert ./images/input/20201110083027_0039.png -crop 10x10@ +repage +adjoin tiles_%d.jpg

convert ./images/input/VideoAE2022_02_11_10_17_29_exported_1900.jpg -crop 10x10@ +repage +adjoin tiles_%d.jpg

convert -crop 256x256 source.png tiles/tile%03d.png

# Process all files in directory, rename and dump in a different dir
for file in wolverton/*.png; convert $file -crop 256x256 tiles.wolverton/${file:t:r}.%03d.png

# process all files matching pattern, padding to provided size
convert profits_*.jpg -resize 960x1568 -background black -gravity center -extent 1568x1568 square.profits.%03d.jpg

split gif

convert animation.gif target.png

will end up with target-0.png, target-1.png etc.

zip 'em up: zip output.zip target*

gm docs

channels

gm convert cmyk.jpg -channel cyan cyan.tiff
gm convert cmyk.jpg -channel magenta magenta.tiff
gm convert cmyk.jpg -channel yellow yellow.tiff
gm convert cmyk.jpg -channel black black.tiff
and then we can join them back together:

gm composite -compose CopyMagenta magenta.tiff cyan.tiff result.tiff
gm composite -compose CopyYellow yellow.tiff result.tiff result.tiff
gm composite -compose CopyBlack black.tiff result.tiff result.tiff

blending from image to image

Script doesn;t work in zsh. Math issues that are beyond me.

./blendshow.sh sluggo*.png | grep ffmpeg | sh

./blendshow.sh ~/Downloads/color_frames/*.png | grep ffmpeg | sh

Setting the t value to 0 doesn't work. At least it ran for 8+ minutes and had only processed 1/10 of the frames But 0.1 works great!

ffmpeg -i example.mkv -c copy example.mp4

ffmpeg -i colors.blend.only.mkv -c copy colors.blend.only.mp4

change file type

mogrify -path '/home/hamy/Documents/JP2_Wrangling/2DArtist_066/Processed' -verbose -quality 95 -format jpg *.jp2

magick mogrify -path './' -verbose  -format png *.jp2

magick mogrify -path './' -verbose -format jpg *.jp2

pdf extraction

mkdir denslow
pdfimages -verbose -j denslow.goose.2003goudy25765.pdf ./denslow

for FILENAME in $(ls *.bpm; do convert $FILENAME ${FILENAME%.*}.png 

for x in *.webp; do dwebp {} -o ${x%.*}.png ::: $x; done

for x in *.bpm; do convert ${x%.*}.png ::: $x; done

convert *.pbm -set filename:fn '%[basename]' '%[filename:fn].png'

convert -density 300 lsi-ii.pdf -quality 100 -alpha remove lsi-ii.png

covert WomboDream "meme style" output into cropped tiles

convert -repage 960x960-60-220 -crop 320x320 +repage *.jpg tile.%03d.jpg && mogrify tile* -shave 4x4 tile.*

should it be 2x2 or 4x4 ???

montages

montage -geometry +0+0 -tile 10x /Users/michaelpaulukonis/projects/images/insta/*.png montage.png

montage -geometry +0+0 -tile 8x /Users/michaelpaulukonis/projects/images/insta/*.png montage.08.png


/Users/michaelpaulukonis/projects/images/insta/

convert -crop 4096x4096 montage.08.png meats.%03d.png

mogrify montage.08.png  -resize 2048 montage.small.png

mogrify -resize 2048x2048 -format jpg /Users/michaelpaulukonis/projects/images/insta/montage.08.png 

mogrify -resize 2048x2048 -format jpg /Users/michaelpaulukonis/projects/images/insta/meats*.png

resizing

smartresize() {
   mogrify -path $3 -filter Triangle -define filter:support=2 -thumbnail $2 -unsharp 0.25x0.08+8.3+0.045 -dither None -posterize 136 -quality 82 -define jpeg:fancy-upsampling=off -define png:compression-filter=5 -define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude-chunk=all -interlace none -colorspace sRGB $1
}
for f in /Users/michaelpaulukonis/projects/images/35.marilyns/*.png;  do smartresize ${f} 300 /Users/michaelpaulukonis/projects/images/marilyns.resized/; done;

smartresize was taken from this article

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment