Skip to content

Instantly share code, notes, and snippets.

@sulram
Last active January 8, 2024 07:39
Show Gist options
  • Save sulram/0c8a95fc90f23e860b9a to your computer and use it in GitHub Desktop.
Save sulram/0c8a95fc90f23e860b9a to your computer and use it in GitHub Desktop.

ImageMagick and FFMPEG commands

Remember that the order of parameters/options is very important.

convert images and save to another dir
convert *.png -set filename:original %t 'export/%[filename:original].jpg'
resize, change dpi and quality
convert *.jpg -resize 1600 -quality 70% -depth 72 -units pixelsperinch resized.jpg
convert *.jpg -set filename:original %t -resize 1600 -quality 70% -depth 72 -units pixelsperinch 'resized_%[filename:original].jpg'
resize and crop
convert *.jpg -resize "460x460^" -gravity center -crop 460x460+0+0 +repage crop.jpg
mogrify -path ../voxels3 -format png -resize "512x512^" -gravity center -crop 512x512+0+0 +repage *.png
resize to fit with background
convert *.png -background black -resize 1024x768 -gravity center -extent 1024x768 fit.png
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
prores
ffmpeg -i aya-mpeg4.mov -c:v prores -profile:v 2 aya-prores.mov
ffmpeg -i aya-mpeg4.mov -vcodec prores_ks -pix_fmt yuva444p10le -alpha_bits 16 -profile:v 4444 -f mov aya-prores-alpha.mov
stretch video
ffmpeg -i input.mp4 -filter_complex "setpts=PTS/10" output.mp4
video from png
ffmpeg -r 8 -i stylegan-%06d.png -c:v libx264 -vf fps=30 -pix_fmt yuv420p latent-space-walker.mp4

How-to: Convert JPG to PDF using ImageMagick

http://catlingmindswipe.blogspot.com.br/2013/11/how-to-convert-jpg-to-pdf-using.html

It's a common enough task, trying to convert multiple jpg files into one pdf, particularly when I don't need these to be converted with such high quality, I just want the black and white text readable.

I can scan, crop, and monochrome in a graphics program but compiling them into a single PDF booklet was always tricky. Which is how I started using ImageMagick.

ImageMagick is a command line conversion program that is capable of so many more batch operations than this - resizing, compression, format conversion - and it's available on all platforms - Linux, Windows and Mac.

Bear in mind that creating a PDF document from multiple JPEG images can take some time and you may want to trial different settings for size and quality of output, so I suggest you make a copy of the JPEG files in a temporary folder to play around with and use Imagemagick on those, NOT your originals. Reducing the resolution as a first step will also make things much quicker.

Originally I was just using the basic

convert *.jpg output.pdf and/or convert *.jpg -adjoin output.pdf

which works most of the time, however there's a bug in the convert routine which can in some versions give a segmentation fault when converting a number of JPEG files to one PDF file.

What this command does is take all the .jpg's (or format of your choice) in a folder and convert them to a single PDF - you can name it whatever you like.

You can avoid the segmentation fault bug and do the compression at the same time if you use

convert *.JPG -compress Zip output.pdf

but the zip compression appears quite inefficient and results in huge file sizes.

You could resize and lower the quality of the images using;

mogrify -resize 50% -quality 25

Which overwrites the originals. You can combine resizing and conversion using

convert -quality 25 -resize 50% *.jpg -adjoin output.pdf

which works, but takes longer as you're combining batch operations.

And yes, you can resizing images without overwriting the originals by specifying a new file name;

convert '*.JPG' -resize 640x480 newfile%03d.jpg

which outputs the converted images as newfile001.jpg, newfile002.jpg, and so on. Alternatively, if you want to retain the original file name and prepend new, you could use a bit more code:

for file in *.JPG ; do convert "$file" -resize 640x480  "new-${file}" ; done

This is just a sample of what Imagemagick can do if you are prepared to experiment at the command line. RC

for img in *.png; do
filename=${img%.*}
convert "$filename.png" "$filename.jpg"
done
@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