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