Skip to content

Instantly share code, notes, and snippets.

@kuncevic
Forked from protrolium/terminal-gif.md
Created June 18, 2019 07:37
Show Gist options
  • Save kuncevic/7baf9607ffbd363f7d5d25a2edbef5bb to your computer and use it in GitHub Desktop.
Save kuncevic/7baf9607ffbd363f7d5d25a2edbef5bb to your computer and use it in GitHub Desktop.
convert images to GIF in Terminal

Install ImageMagick

brew install ImageMagick

Pull specific region of frames from video file w/ ffmpeg

ffmpeg -ss 14:55 -i video.mkv -t 5 -s 480x270 -f image2 %04d.png

  • -ss 14:55 gives the timestamp where I want FFmpeg to start, as a duration string.
  • -t 5 says how much I want FFmpeg to decode, using the same duration syntax as for -ss.
  • -s 480x270 tells FFmpeg to resize the video output to 480 by 270 pixels.
  • -f image2 selects the output format, a series of still images — make sure there are leading zeros in filename.

Resize series of images

convert *.png -resize 70% newname%02d.png

Convert mutliple filetype(s) into animated gif

convert -delay 10 -loop 0 *.png anim.gif
convert -fuzz 1% -delay 1x8 *.png -coalesce -layers OptimizeTransparency animation.gif

  • -fuzz tells ImageMagick to treat pixels whose color values differ by less than 1% as the same color, giving the OptimizeTransparency action more pixels to chop away.
  • -delay 1x8 says that the animation should play a frame every 1/8 of a second.
  • -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.

convert -fuzz 1% -delay 10 -loop 0 *.png -coalesce -layers OptimizeTransparency animation.gif

Ping-pong Looping Method

convert script_k.gif -coalesce -duplicate 1,-2-1 -quiet -layers OptimizePlus -loop 0 patrol_cycle.gif

hypervoid force sizing + optimization:
convert -resize '1920x350!' -delay 5 -loop 0 *.png hv.gif
convert -fuzz 3% -resize '1920x350!' -delay 5 -loop 0 -coalesce -layers OptimizeTransparency *.png an.gif

this test yielded impressive reduction:
convert -filter Triangle -define filter:support=2 -thumbnail 1920 -delay 5 -loop 0 -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 *.png output.gif

another version with less color information
convert -filter Triangle -define filter:support=2 -thumbnail 1920 -delay 5 -loop 0 -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 -colors 32 -ordered-dither o8x8,8,8,8,4 +map *.png output.gif

convert GIF into movie by extracting frames and recompiling

convert -coalesce some.gif some%05d.png
extract gif frames

ffmpeg -i some%05d.png some.mov
converting image sequence in QuickTime 7 worked more effectively than the above command

rm some*.png
clear extracted frames

Pull apart GIF

convert -coalesce animation.gif target.png

Cropping / Batch Crop

If you want the crop rectangle to start at top corner X: 50 Y: 100 and the crop rectangle to be of size W: 640 H:480, then use the command:
$ mogrify -crop 640x480+50+100 foo.png

To write the cropped image to a new file:
$ convert foo.png -crop 640x480+50+100 out.png

batch conversion:
for i in *.png; do convert "$i" -crop 1920x248+0+400 "${i%.png}-cropped.png"; done


more information about the convert command
and here
and here

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