2 title: gify.sh – back to bash scripting
5 date: 2016-01-25T12:41:06+00:00
6 url: /2016/01/gify-back-to-bash-scripting/
7 featured_image: /wp-content/uploads/2016/01/G0092546.jpg
21 <div class="wp-block-image size-full wp-image-3645">
22 <figure class="aligncenter"><img loading="lazy" width="900" height="675" src="https://danix.xyz/wp-content/uploads/2016/01/piscaturi.gif" alt="" class="wp-image-3645" /><figcaption>gif image created using gify.sh</figcaption></figure>
25 Today I’ll present you a useful script that will help you create amazing gifs from your still photos using a couple tools from the <a href="https://www.imagemagick.org" target="_blank" rel="noopener noreferrer">IMAGEMAGIK</a> suite, so without further ado, here it is, straight from github’s gists.
27 <div class="wp-block-buttons aligncenter">
28 <div class="wp-block-button">
29 <a class="wp-block-button__link" href="" target="_blank" rel="noreferrer noopener">download in zip format</a>
33 what the code does is quite simple, it takes a few arguments and helps you resize your images while keeping the original proportions. Put the code in your path and let’s put it to work.
37 For this example we’ll pretend you have a bunch of jpg pictures, maybe something you shot while travelling france, put them in a folder and run the script like this:
39 <pre class="wp-block-code language-bash"><code>gify.sh --resize 900 jpg</code></pre>
41 depending on the amount of images it may take a while, and when it’s done you’ll have all of your jpg resized to 900px wide and with proportional height.
43 now it’s time to render all of your jpg into one animated gif so, while in the same directory, run the script like this:
45 <pre class="wp-block-code language-bash"><code>gify.sh --gif 10 jpg paris</code></pre>
47 the parameters now are a bit different, first we tell the script that we want to create a gif with the `--gif` option, then we tell the interval between every frame, that number is expressed in 100th of a second, so now we are telling the gif to change frame every 10/100 of second, the following parameter is the extension of the images we’ll be using, in our example is jpg (it’s case sensitive, so make sure all the images you want to use have the same extension), and finally we set the name of the gif, without the extension, the script will provide it for us.
49 The gif you see at the beginning of the article was created with this script.
51 I made this script mostly for fun and personal use, so it’s absolutely not idiot proof, can be improved but for me it works just fine. If you want to modify it, feel free to fork it on github or just download it and do whatever you like with it, and if you want to share it with me, use the comment form below.
53 I’ll leave you with a copy of the script here in case you want to have a look at it before downloading.
56 <pre class="wp-block-code language-bash"><code>#! /bin/bash
58 # Author: Danilo 'danix' Macri
59 # Author URI: https://danix.xyz
60 # Script URI: https://danix.xyz/?p=3545
62 # License URI: https://www.gnu.org/licenses/gpl-2.0.html
64 #--------------------------------------------------------------------------------#
66 # GIFY.SH - CREATE ANIMATED GIFS OUT OF A BUNCH OF IMAGES #
68 # Use this script to create animated looping gifs from a bunch of images. You #
69 # just need to arrange all the images you want to use inside a folder and then #
70 # launch this script with a few options and you'll have your gif within seconds. #
72 # This script uses mogrify and convert from the IMAGEMAGIK suite to deliver the #
73 # gifs. This script can proportionally resize your images to help you create a #
76 #--------------------------------------------------------------------------------#
78 # ERROR & EXIT STATUSES
91 MOGRIFY=$(which mogrify)
92 CONVERT=$(which convert)
94 # we need mogrify and convert from the imagemagik toolset for this script to work
95 if [[ ! -x $MOGRIFY || ! -x $CONVERT ]]; then
106 echo "USAGE: $(basename $0) -r | --resize [width] [extension]"
109 echo "USAGE: $(basename $0) -g | --gif [delay] [extension] [output file name]"
112 #|----------------------- TEXT MAX WIDTH - 80 CHARS ----------------------------|
113 echo -e "$(basename $0) - create animated gifs from images inside current directory"
114 echo -e "USAGE: $(basename $0) <option> [arguments]"
115 echo -e "\twhere <option> is one between:";echo
116 echo -e "\t-r | --resize [width] [extension]"
117 echo -e "\t\tresizes all the images matching the extension in the current folder to"
118 echo -e "\t\tthe width specified as argument.";echo
119 echo -e "\tg | --gif [delay] [extension] [output file name]"
120 echo -e "\t\tcreates the gif file using all the images in the current folder."
123 echo -e "$(basename $0) --resize 900 jpg"
124 echo -e "\twill resize all jpg images in the folder to 900px wide and mantain the"
125 echo -e "\taspect ratio of the original images"
127 echo -e "$(basename $0) --gif 8 jpg france"
128 echo -e "\twill create a looping gif named france.gif using all the jpg files found"
129 echo -e "\tin the current folder and passing a tick delay of 8 between frames".
138 if [ -z $1 ];then
139 echo "INTERNAL ERROR - ABORTING"; echo
144 echo "unknown option. Exiting."; echo
147 echo "you didn't specify any options for the script to run. Exiting."; echo
150 echo "you didn't specify any arguments for this option. Exiting."; echo
153 echo "the file you want to write already exists. Exiting."; echo
156 echo "at least two files must exist within $PWD with the"
157 echo "specified extension. Exiting"; echo
160 echo "$(basename $0) requires both mogrify and convert from"
161 echo "the imagemagik tool suite. Install imagemagik using your"
162 echo "favourite package manager and then run this script again. Exiting."; echo
167 if [ $# -eq 0 ];then
173 while [ $# -gt 0 ];do
183 if [[ -z $WIDTH || -z $EXT ]];then
188 IMAGES="$(ls -1 *.$EXT 2>/dev/null | wc -l)"
189 if [[ $IMAGES == 0 ]]; then
194 COUNT="$(ls -1 *.$EXT 2>/dev/null | wc -l)"
195 echo "you're going to resize all $COUNT .$EXT images inside $PWD at a fixed width of ${WIDTH}px"
196 read -p "do you wish to continue? [y/n] " -n 1 -r; echo
197 if [[ ! $REPLY =~ ^[Yy]$ ]]
201 $MOGRIFY -resize $WIDTH *.$EXT
210 if [[ -z $DELAY || -z $EXT || -z $OUTPUT ]];then
214 elif [[ -f ${OUTPUT}.gif ]]; then
218 IMAGES="$(ls -1 *.$EXT 2>/dev/null | wc -l)"
219 if [[ $IMAGES == 0 ]]; then
224 COUNT="$(ls -1 *.$EXT 2>/dev/null | wc -l)"
225 echo "you're going to create a looping gif named ${OUTPUT}.gif"
226 echo "out of all the $COUNT $EXT files inside $PWD with a tick"
227 echo "delay of $DELAY/100 of a second"; echo
228 read -p "do you wish to continue? [y/n] " -n 1 -r; echo
229 if [[ ! $REPLY =~ ^[Yy]$ ]]
233 $CONVERT -delay $DELAY *.$EXT -loop 0 ${OUTPUT}.gif