Skip to content

Instantly share code, notes, and snippets.

@sulram
Last active April 15, 2025 00:03
Show Gist options
  • Select an option

  • Save sulram/0c8a95fc90f23e860b9a to your computer and use it in GitHub Desktop.

Select an option

Save sulram/0c8a95fc90f23e860b9a to your computer and use it in GitHub Desktop.

ImageMagick + FFMPEG commands

resize, change dpi and quality

convert *.jpg -resize 1600 -quality 70% -depth 72 -units pixelsperinch resized.jpg

resize and crop

convert *.jpg -resize "460x460^" -gravity center -crop 460x460+0+0 +repage crop.jpg

mosaic with cropped images

montage crop*.jpg -geometry 460x460+0+0 mosaic.jpg

scale and stack images

convert *.png -append -scale 50% stacked.png

animated gif from png files

convert -delay 5 -loop 0 *.png ../animation.gif

from mov to gif (with ffmpeg)

thanks to http://blog.room208.org/post/48793543478

1. convert video to PNG sequence with FFMPEG

ffmpeg -i video.mov -r 10 -s 640x400 -f image2 frames/frame-%03d.png
  • -i video.mov the video file
  • -r 10 the rate
  • -s 640x400 the output size
  • -f image2 the output format, a series of still images
  • frames/frame-%03d.png is a printf format string specifying the output filenames, in this case dir/name-###.png, a series of PNG images called 001.png, 002.png, 003.png, and so on.

2. convert PNG sequence to animated GIF

convert frames/*.png -delay 1x8 -coalesce -layers OptimizeTransparency animation.gif
  • frames/*.png input files
  • -delay 1x8 says that the animation should play a frame every 1/8 of a second. I computed this number by looking at the frame rate of the original video (24) and dividing by the number of frames each drawing plays for (3). Note that most browsers slow down animations that play faster than 20 frames per second, or 1/50 second per frame. Most videos play back at between 25 and 30 fps, so you may have to drop every other frame or so if you care about accuracy of playback speed.
  • -coalesce apparently “fully define[s] the look of each frame of an [sic] GIF animation sequence, to form a ‘film strip’ animation,” according to the documentation.
  • -layers OptimizeTransparency tells ImageMagick to replace portions of each frame that are identical to the corresponding parts of the preceding frame with transparency, saving on file size.
  • animation.gif output file

convert gif to images and video

convert ani.gif -coalesce ani.png
ffmpeg -f image2 -i ani-%d.png ani.mpg -vb 20M
@sulram
Copy link
Author

sulram commented May 31, 2018

# modify images in place and make images larger than 1280x1280 pixels smaller
mogrify -filter lanczos2 -resize '1280x1280>' *.png

# save thumbnails to ~/Desktop and make images wider than 500 pixels smaller
mogrify -filter lanczos2 -thumbnail 'x500>' -format jpg -quality 93 -path ~/Desktop/ *.png

# make images smaller or larger and crop them so that they are exactly 200x200 pixels
-resize 200x200^ -extent 200x200 -gravity center

# use a white instead of a black background
convert transparent-bg.png -flatten white-bg.jpg

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