added various articles from the previous version of my site.
[danix.xyz.git] / articles / gify-back-to-bash-scripting.md
1 ---
2 title: gify.sh – back to bash scripting
3 author: Danilo M.
4 type: post
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
8 categories:
9 - code
10 - diy
11 - fotografia
12 tags:
13 - bash
14 - convert
15 - gif
16 - imagemagik
17 - mogrify
18 - script
19
20 ---
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>
23 </div>
24
25 Today I&#8217;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&#8217;s gists.
26
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>
30 </div>
31 </div>
32
33 what the code does is quite simple, it takes a few arguments and helps you resize your images&nbsp;while keeping the original proportions. Put the code in your path and let&#8217;s put it to work.
34
35 <!--more-->
36
37 For this example we&#8217;ll pretend you have a bunch of jpg pictures, maybe something you shot while travelling france,&nbsp;put&nbsp;them&nbsp;in a folder and run the script like this:
38
39 <pre class="wp-block-code language-bash"><code>gify.sh --resize 900 jpg</code></pre>
40
41 depending on the amount of images it may take a while, and when it&#8217;s done you&#8217;ll have all of your jpg resized to 900px wide and with proportional height.
42
43 now it&#8217;s time to render all of your jpg into one animated gif so, while in the same directory, run the script like this:
44
45 <pre class="wp-block-code language-bash"><code>gify.sh --gif 10 jpg paris</code></pre>
46
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&#8217;ll be using, in our example is jpg (it&#8217;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.
48
49 The gif you see at the beginning of the article was created with this script.
50
51 I made this script mostly for fun and personal use, so it&#8217;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.
52
53 I&#8217;ll leave you with a copy of the script here in case you want to have a look at it before downloading.
54 Enjoy!
55
56 <pre class="wp-block-code language-bash"><code>#! /bin/bash
57
58 # Author: Danilo 'danix' Macri
59 # Author URI: https://danix.xyz
60 # Script URI: https://danix.xyz/?p=3545
61 # License: GPL2
62 # License URI: https://www.gnu.org/licenses/gpl-2.0.html
63
64 #--------------------------------------------------------------------------------#
65 # #
66 # GIFY.SH - CREATE ANIMATED GIFS OUT OF A BUNCH OF IMAGES #
67 # #
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. #
71 # #
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 #
74 # lighter file. #
75 # #
76 #--------------------------------------------------------------------------------#
77
78 # ERROR & EXIT STATUSES
79 SHOWHELP=61
80 USERABORTED=62
81
82 E_INTERROR=71
83 E_NOOPTS=72
84 E_NOARGS=73
85 E_FILEXISTS=74
86 E_NOIMAGES=75
87 E_UNKNOWNOPT=76
88
89 # TOOLS
90 PWD=$(pwd)
91 MOGRIFY=$(which mogrify)
92 CONVERT=$(which convert)
93
94 # we need mogrify and convert from the imagemagik toolset for this script to work
95 if &#91;&#91; ! -x $MOGRIFY || ! -x $CONVERT ]]; then
96 showerror missingdeps
97 exit $E_MISSINGDEPS
98 fi
99
100
101 # showhelp
102 showhelp ()
103 {
104 case $1 in
105 resize )
106 echo "USAGE: $(basename $0) -r | --resize &#91;width] &#91;extension]"
107 ;;
108 gif )
109 echo "USAGE: $(basename $0) -g | --gif &#91;delay] &#91;extension] &#91;output file name]"
110 ;;
111 * )
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) &lt;option> &#91;arguments]"
115 echo -e "\twhere &lt;option> is one between:";echo
116 echo -e "\t-r | --resize &#91;width] &#91;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 &#91;delay] &#91;extension] &#91;output file name]"
120 echo -e "\t\tcreates the gif file using all the images in the current folder."
121 echo
122 echo -e "EXAMPLES:"
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"
126 echo
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".
130 echo
131 ;;
132 esac
133 }
134
135 # showerror
136 showerror ()
137 {
138 if &#91; -z $1 ];then
139 echo "INTERNAL ERROR - ABORTING"; echo
140 exit $E_INTERROR
141 fi
142 case $1 in
143 unknownopt)
144 echo "unknown option. Exiting."; echo
145 ;;
146 noopts)
147 echo "you didn't specify any options for the script to run. Exiting."; echo
148 ;;
149 noargs)
150 echo "you didn't specify any arguments for this option. Exiting."; echo
151 ;;
152 filexists)
153 echo "the file you want to write already exists. Exiting."; echo
154 ;;
155 noimages)
156 echo "at least two files must exist within $PWD with the"
157 echo "specified extension. Exiting"; echo
158 ;;
159 missingdeps)
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
163 esac
164 }
165
166 ##### MAIN #####
167 if &#91; $# -eq 0 ];then
168 showerror noopts
169 showhelp
170 exit $E_NOOPTS
171 else
172
173 while &#91; $# -gt 0 ];do
174 case $1 in
175 -h|--help)
176 showhelp
177 exit $SHOWHELP
178 ;;
179 -r|--resize)
180 WIDTH=$2
181 EXT=$3
182 shift
183 if &#91;&#91; -z $WIDTH || -z $EXT ]];then
184 showhelp resize
185 showerror noargs
186 exit $E_NOARGS
187 fi
188 IMAGES="$(ls -1 *.$EXT 2>/dev/null | wc -l)"
189 if &#91;&#91; $IMAGES == 0 ]]; then
190 showerror noimages
191 exit $E_NOIMAGES
192 fi
193 clear
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? &#91;y/n] " -n 1 -r; echo
197 if &#91;&#91; ! $REPLY =~ ^&#91;Yy]$ ]]
198 then
199 exit $USERABORTED
200 else
201 $MOGRIFY -resize $WIDTH *.$EXT
202 exit 0
203 fi
204 ;;
205 -g|--gif)
206 DELAY=$2
207 EXT=$3
208 OUTPUT=$4
209 shift
210 if &#91;&#91; -z $DELAY || -z $EXT || -z $OUTPUT ]];then
211 showhelp gif
212 showerror noargs
213 exit $E_NOARGS
214 elif &#91;&#91; -f ${OUTPUT}.gif ]]; then
215 showerror filexists
216 exit $E_FILEXISTS
217 fi
218 IMAGES="$(ls -1 *.$EXT 2>/dev/null | wc -l)"
219 if &#91;&#91; $IMAGES == 0 ]]; then
220 showerror noimages
221 exit $E_NOIMAGES
222 fi
223 clear
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? &#91;y/n] " -n 1 -r; echo
229 if &#91;&#91; ! $REPLY =~ ^&#91;Yy]$ ]]
230 then
231 exit $USERABORTED
232 else
233 $CONVERT -delay $DELAY *.$EXT -loop 0 ${OUTPUT}.gif
234 exit 0
235 fi
236 ;;
237 *)
238 showerror unknownopt
239 showhelp
240 exit $E_UNKNOWNOPT
241 esac
242 shift
243 done
244 fi</code></pre>