Manipulating Images and Videos

This page is a resource filled with commands and tools to manipulate and create images and videos

ImageMagick

Image Magick is a useful tool to convert images/videos between different file types. First, install ImageMagick from the website (and it may be necessary to install this postscript and pdf interpreter as well). After this, typing magick in the command prompt should not yield an error.

Converting file types

Simply type magick followed by an input file and an output file (with different file extension). For example:

magick file1.png file2.jpg

Creating GIFs

Magick can convert a sequence of images given in a PDF to an output GIF magick -density n1 -dispose n2 -delay t images.pdf out.gif

  • n1 controls the pixel density of the image (dpi) and 200 should be sufficient
  • n2 controls how we dispose of a previous image (1: overlay, 2: clear frame area with background color, 3: clear image prior to frame overlay (usually what I want))
  • t is the time (in centiseconds) between each frame

Composing many images into 1 PDF

To combine N jpeg files into one single pdf type in

magick file1.jpeg ... fileN.jpef combined_file.pdf

  • Can do something like *.jpeg instead to combine all files in current directory.

Extract frames from GIF

If we have a GIF composed of many frames, we can extract each frame and store each individually as a PNG

magick name.gif -coalesce out_name_%d.png

FFMPEG

FFMPEG is able to decode, encode, transcode, mux, demux, stream, filter and play pretty much anything that humans and machines have created. It is already installed on OSC and can be used by typing in

module load ffmpeg

Below, are some of the most commmon uses for ffmpeg that I have encountered

Converting Images to Videos

Given a set of n images indexed in order numerically (e.g. 00.png $\rightarrow$ 09.png), we can create a video with the following code

ffmpeg -f image2 -pattern_type glob -framerate fr -i './*.png' -s WxH vid_name.avi

  • fr should be replaced with the desired framerate
  • W and H should be replaced with a desired width and height (e.g. 1200x900)
  • Make sure that the files are ordered sequentially in to ensure the *.png specification works
Written on September 26, 2023