Creating a GIF from a series of images is a simple process with the right tools. This bash script simplifies the process using FFmpeg, a popular multimedia tool for handling multimedia data.
Here's how to use the script:
./create-gif.sh <path-to-images> [pattern] [output] [fps]
path-to-images
(required): The path to the folder containing the images you want to convert into a GIF.pattern
(optional): The pattern to use to find the images. By default, the script uses '%d.png'.output
(optional): The output file name. By default, the script uses the folder name + .gif.fps
(optional): The frames per second (FPS) for the output. By default, the script uses 10 FPS.
Here are some examples to help you get started:
./create-gif.sh /path/to/images
This command converts all the images in the '/path/to/images' folder to a GIF with a default name, which is the folder name + '.gif', and default FPS, which is 10 FPS.
./create-gif.sh /path/to/images 'image-%d.png'
This command converts all the images in the '/path/to/images' folder that follow the 'image-%d.png' pattern to a GIF with a default name and default FPS.
./create-gif.sh /path/to/images 'image-%d.png' output.gif
This command converts all the images in the '/path/to/images' folder that follow the 'image-%d.png' pattern to a GIF with a specified output file name, 'output.gif', and default FPS.
Example 4: Convert images in a folder with a pattern to a specified output file with a specified FPS
./create-gif.sh /path/to/images 'image-%d.png' output.gif 15
This command converts all the images in the '/path/to/images' folder that follow the 'image-%d.png' pattern to a GIF with a specified output file name, 'output.gif', and specified FPS of 15.
The script starts by checking if the input is a folder, and if not, it displays an error message and exits. Next, it sets the pattern and output filenames based on the arguments or default values. It then creates a color palette for the GIF using the palettegen
filter in FFmpeg. The palettegen
filter creates a color palette that contains all the colors used in the images.
After creating the palette, the script uses the paletteuse
filter to map the image colors to the palette colors. The paletteuse
filter applies the color palette to the input images and creates the final GIF. Finally, the script removes the palette file to clean up.
One thing to note is that the palettegen
filter generates a palette file that is used by the paletteuse
filter. The script stores this file in the input folder with the name 'palette.png'. If you run the script multiple times in the same folder, it will overwrite the existing palette file. It is essential to keep this in mind when using the script.