6737a632d062e12f5bcf6d585d32d82583429b92
[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 featured_image: /uploads/2016/01/G0092546.jpg
7 categories:
8 - code
9 - diy
10 - fotografia
11 tags:
12 - bash
13 - convert
14 - gif
15 - imagemagik
16 - mogrify
17 - script
18
19 ---
20 <div class="wp-block-image size-full wp-image-3645">
21 <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>
22 </div>
23
24 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.
25
26 <div class="wp-block-buttons aligncenter">
27 <div class="wp-block-button">
28 <a class="wp-block-button__link" href="" target="_blank" rel="noreferrer noopener">download in zip format</a>
29 </div>
30 </div>
31
32 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.
33
34 <!--more-->
35
36 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:
37
38 <pre class="wp-block-code language-bash"><code>gify.sh --resize 900 jpg</code></pre>
39
40 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.
41
42 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:
43
44 <pre class="wp-block-code language-bash"><code>gify.sh --gif 10 jpg paris</code></pre>
45
46 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.
47
48 The gif you see at the beginning of the article was created with this script.
49
50 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.
51
52 I&#8217;ll leave you with a copy of the script here in case you want to have a look at it before downloading.
53 Enjoy!
54
55 {{< highlight bash "linenos=true" >}}#! /bin/bash
56
57 # Author: Danilo 'danix' Macri
58 # Author URI: https://danix.xyz
59 # Script URI: https://danix.xyz/?p=3545
60 # License: GPL2
61 # License URI: https://www.gnu.org/licenses/gpl-2.0.html
62
63 #--------------------------------------------------------------------------------#
64 # #
65 # GIFY.SH - CREATE ANIMATED GIFS OUT OF A BUNCH OF IMAGES #
66 # #
67 # Use this script to create animated looping gifs from a bunch of images. You #
68 # just need to arrange all the images you want to use inside a folder and then #
69 # launch this script with a few options and you'll have your gif within seconds. #
70 # #
71 # This script uses mogrify and convert from the IMAGEMAGIK suite to deliver the #
72 # gifs. This script can proportionally resize your images to help you create a #
73 # lighter file. #
74 # #
75 #--------------------------------------------------------------------------------#
76
77 # ERROR & EXIT STATUSES
78 SHOWHELP=61
79 USERABORTED=62
80
81 E_INTERROR=71
82 E_NOOPTS=72
83 E_NOARGS=73
84 E_FILEXISTS=74
85 E_NOIMAGES=75
86 E_UNKNOWNOPT=76
87
88 # TOOLS
89 PWD=$(pwd)
90 MOGRIFY=$(which mogrify)
91 CONVERT=$(which convert)
92
93 # we need mogrify and convert from the imagemagik toolset for this script to work
94 if &#91;&#91; ! -x $MOGRIFY || ! -x $CONVERT ]]; then
95 showerror missingdeps
96 exit $E_MISSINGDEPS
97 fi
98
99
100 # showhelp
101 showhelp ()
102 {
103 case $1 in
104 resize )
105 echo "USAGE: $(basename $0) -r | --resize &#91;width] &#91;extension]"
106 ;;
107 gif )
108 echo "USAGE: $(basename $0) -g | --gif &#91;delay] &#91;extension] &#91;output file name]"
109 ;;
110 * )
111 #|----------------------- TEXT MAX WIDTH - 80 CHARS ----------------------------|
112 echo -e "$(basename $0) - create animated gifs from images inside current directory"
113 echo -e "USAGE: $(basename $0) &lt;option> &#91;arguments]"
114 echo -e "\twhere &lt;option> is one between:";echo
115 echo -e "\t-r | --resize &#91;width] &#91;extension]"
116 echo -e "\t\tresizes all the images matching the extension in the current folder to"
117 echo -e "\t\tthe width specified as argument.";echo
118 echo -e "\tg | --gif &#91;delay] &#91;extension] &#91;output file name]"
119 echo -e "\t\tcreates the gif file using all the images in the current folder."
120 echo
121 echo -e "EXAMPLES:"
122 echo -e "$(basename $0) --resize 900 jpg"
123 echo -e "\twill resize all jpg images in the folder to 900px wide and mantain the"
124 echo -e "\taspect ratio of the original images"
125 echo
126 echo -e "$(basename $0) --gif 8 jpg france"
127 echo -e "\twill create a looping gif named france.gif using all the jpg files found"
128 echo -e "\tin the current folder and passing a tick delay of 8 between frames".
129 echo
130 ;;
131 esac
132 }
133
134 # showerror
135 showerror ()
136 {
137 if &#91; -z $1 ];then
138 echo "INTERNAL ERROR - ABORTING"; echo
139 exit $E_INTERROR
140 fi
141 case $1 in
142 unknownopt)
143 echo "unknown option. Exiting."; echo
144 ;;
145 noopts)
146 echo "you didn't specify any options for the script to run. Exiting."; echo
147 ;;
148 noargs)
149 echo "you didn't specify any arguments for this option. Exiting."; echo
150 ;;
151 filexists)
152 echo "the file you want to write already exists. Exiting."; echo
153 ;;
154 noimages)
155 echo "at least two files must exist within $PWD with the"
156 echo "specified extension. Exiting"; echo
157 ;;
158 missingdeps)
159 echo "$(basename $0) requires both mogrify and convert from"
160 echo "the imagemagik tool suite. Install imagemagik using your"
161 echo "favourite package manager and then run this script again. Exiting."; echo
162 esac
163 }
164
165 ##### MAIN #####
166 if &#91; $# -eq 0 ];then
167 showerror noopts
168 showhelp
169 exit $E_NOOPTS
170 else
171
172 while &#91; $# -gt 0 ];do
173 case $1 in
174 -h|--help)
175 showhelp
176 exit $SHOWHELP
177 ;;
178 -r|--resize)
179 WIDTH=$2
180 EXT=$3
181 shift
182 if &#91;&#91; -z $WIDTH || -z $EXT ]];then
183 showhelp resize
184 showerror noargs
185 exit $E_NOARGS
186 fi
187 IMAGES="$(ls -1 *.$EXT 2>/dev/null | wc -l)"
188 if &#91;&#91; $IMAGES == 0 ]]; then
189 showerror noimages
190 exit $E_NOIMAGES
191 fi
192 clear
193 COUNT="$(ls -1 *.$EXT 2>/dev/null | wc -l)"
194 echo "you're going to resize all $COUNT .$EXT images inside $PWD at a fixed width of ${WIDTH}px"
195 read -p "do you wish to continue? &#91;y/n] " -n 1 -r; echo
196 if &#91;&#91; ! $REPLY =~ ^&#91;Yy]$ ]]
197 then
198 exit $USERABORTED
199 else
200 $MOGRIFY -resize $WIDTH *.$EXT
201 exit 0
202 fi
203 ;;
204 -g|--gif)
205 DELAY=$2
206 EXT=$3
207 OUTPUT=$4
208 shift
209 if &#91;&#91; -z $DELAY || -z $EXT || -z $OUTPUT ]];then
210 showhelp gif
211 showerror noargs
212 exit $E_NOARGS
213 elif &#91;&#91; -f ${OUTPUT}.gif ]]; then
214 showerror filexists
215 exit $E_FILEXISTS
216 fi
217 IMAGES="$(ls -1 *.$EXT 2>/dev/null | wc -l)"
218 if &#91;&#91; $IMAGES == 0 ]]; then
219 showerror noimages
220 exit $E_NOIMAGES
221 fi
222 clear
223 COUNT="$(ls -1 *.$EXT 2>/dev/null | wc -l)"
224 echo "you're going to create a looping gif named ${OUTPUT}.gif"
225 echo "out of all the $COUNT $EXT files inside $PWD with a tick"
226 echo "delay of $DELAY/100 of a second"; echo
227 read -p "do you wish to continue? &#91;y/n] " -n 1 -r; echo
228 if &#91;&#91; ! $REPLY =~ ^&#91;Yy]$ ]]
229 then
230 exit $USERABORTED
231 else
232 $CONVERT -delay $DELAY *.$EXT -loop 0 ${OUTPUT}.gif
233 exit 0
234 fi
235 ;;
236 *)
237 showerror unknownopt
238 showhelp
239 exit $E_UNKNOWNOPT
240 esac
241 shift
242 done
243 fi
244
245 {{< /highlight >}}