{"id":1884,"date":"2015-10-19T15:26:57","date_gmt":"2015-10-19T14:26:57","guid":{"rendered":"http:\/\/johnjohnston.info\/106\/?p=1884"},"modified":"2015-10-20T10:46:45","modified_gmt":"2015-10-20T09:46:45","slug":"taking-command-of-gifs","status":"publish","type":"post","link":"https:\/\/johnjohnston.info\/106\/taking-command-of-gifs\/","title":{"rendered":"Taking Command of Gifs"},"content":{"rendered":"<p>These are some notes on creating gifs and manipulating images on the command line on a mac. I am pretty much at the limit of my knowledge here so some of the details may be sketchy. It seems to me that this allows one to iterate through various ideas quite quickly. I do often take the result into Fireworks for some tweaking of colour palettes.<\/p>\n<p><a href=\"http:\/\/www.digitalnz.org\/gif-it-up\">GIF IT UP &#8211; DigitalNZ<\/a> is on.<\/p>\n<blockquote><p>Calling all GIF-makers, creatives, history nuts, and animators! GIF IT UP is a challenge coordinated by DigitalNZ and the Digital Public Library of America to find the best GIFs created from copyright-free heritage material.<\/p><\/blockquote>\n<p>The early entries look formidable <a href=\"http:\/\/gifitup2015.tumblr.com\/\">GIF IT UP 2015<\/a>, but I am <a href=\"http:\/\/jjgifs.tumblr.com\/tagged\/gifitup2015\">trying out a few things<\/a> in the hope of getting to something worth entering.<\/p>\n<p>One of the suggested sites for source material is <a href=\"http:\/\/www.europeana.eu\/portal\/myeuropeana#login\">Europeana<\/a> and I found a nice set of images: <a href=\"http:\/\/www.europeana.eu\/portal\/search.html?query=Book+on+Swordsmanship+and+Wrestling&amp;rows=96&amp;start=-23&amp;qt=false\">Book on Swordsmanship and Wrestling<\/a> from the mid 15th century. I though a selection might make a nice flip-book. Here is the process:<\/p>\n<h3>Gather the images<\/h3>\n<p>This was a pretty manual process. I decided to grab the ones for fencing. Loaded each pages in the browser then right clicked and downloaded all of the jpgs to a folder.<\/p>\n<h3>Resizing images<\/h3>\n<p>I now start to use the terminal.<\/p>\n<p>To move into the folder of downloaded jpegs I type cd and then drag in the folder, which gives me:<\/p>\n<p><code>cd \/Users\/john\/Desktop\/DS106GifitUP15\/swords2<\/code><\/p>\n<p>I hit return and I am in the right folder to do some work.<\/p>\n<p>Then I make a new folder for smaller pictures:<br \/>\n<code>mkdir 500<\/code><br \/>\nThis produces a folder called 500.<\/p>\n<p>Next resize all the downloaded jpgs to smaller versions in the 500 folder:<\/p>\n<p><code>for i in *.jpg; do sips --resampleWidth 500 $i --out 500\/$i;done<\/code><\/p>\n<p>What this does:<br \/>\n<code>for i in *.jpg<\/code> for every jpg<\/p>\n<p><code>do sips --resampleWidth 500 $i --out 500\/$i;<\/code> sips in an image manipulating programme build into OS X, the $i variable refers in turn to each file. so for a file <em>5055078.jpg<\/em> we are doing this:<\/p>\n<p><code>sips --resampleWidth 500 5055078.jpg --out 500\/5055078.jpg<\/code><\/p>\n<p><code>done<\/code> lets the loop know where to finish.<\/p>\n<p><em>For all the jpgs, resize to 500 pixels wide putting resized version in the 500 folder with the same name as the source file<\/em>. The semicolons separate sections of code creating a loop.<\/p>\n<h3>Making Gifs<\/h3>\n<p>I then <code>cd 500<\/code> to move into the new folder and <code>mkdir gifs<\/code> to create a folder for gif files. Then:<\/p>\n<p><code>for i in *.jpg; do sips -s format gif $i --out gifs\/$i.gif;done<\/code><\/p>\n<p>Which uses sips to create gifs from the jpgs, name like <em>5055078.jpg.gif<\/em> Not the most beautiful names but these are only temporary files.<\/p>\n<h3>Animation<\/h3>\n<p>Now I had 49 single gifs, not yet animated. To animate them I use <a href=\"https:\/\/www.lcdf.org\/gifsicle\/\">Gifsicle<\/a>. This needs to be installed, there is an installer on that link or it can be installed through homebrew <a id=\"fnr1-2015-10-18\" href=\"#fn1-2015-10-18\"><sup>1<\/sup><\/a><\/p>\n<p>49 frames is a few too many I decided to try 11. Gifsicle had a lot of options, I just used a few typing: <code>gifsicle --delay 20 --colors 64 --loop <\/code> then dragging 11 single frame gifs into the terminal window to add them typing <code>-o fight.gif<\/code> This looks pretty messy but hitting return gives me a gif fight in the current directory (500).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1885\" src=\"https:\/\/johnjohnston.info\/106\/wp-content\/uploads\/2015\/10\/fight.gif\" alt=\"fight\" width=\"500\" height=\"395\" \/><\/p>\n<h3>Montage<\/h3>\n<p>The next idea I had was to have several fights going on at the same time. For this I needed to make a set of images made up of 4 of the original images in a 2&#215;2 grid.<\/p>\n<p>There is a commandline application montage <a id=\"fnr2-2015-10-19\" href=\"#fn2-2015-10-19\"><sup>2<\/sup><\/a> that is installed as part of <a href=\"http:\/\/www.imagemagick.org\/script\/index.php\">ImageMagick<\/a> This can be installed via a download or with homebrew (or macports). Homebrew will take care of installing any dependencies as it goes.<\/p>\n<p>Basic use of montage might be:<br \/>\n<code>montage -tile 2x -geometry +0+0 5055069.jpg 5055123.jpg 5055228.jpg 5055280.jpg montageexample.jpeg<\/code><\/p>\n<p>If you were in a folder containing images named 5055069.jpg, 5055123.jpg, 5055228.jpg and 5055280.jpg the <code>-tile 2x<\/code> parameter tells montage to make a 2x wide grid, <code>-geometry +0+0 <\/code> means there is no gsap between images and seems to stop the auto thumbnailing.<br \/>\nThis gives you an image like this:<\/p>\n<p><a href=\"http:\/\/johnjohnston.info\/106\/wp-content\/uploads\/2015\/10\/5055262.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1888 size-medium\" src=\"https:\/\/johnjohnston.info\/106\/wp-content\/uploads\/2015\/10\/5055262-300x237.jpg\" alt=\"5055262\" width=\"300\" height=\"237\" srcset=\"https:\/\/johnjohnston.info\/106\/wp-content\/uploads\/2015\/10\/5055262-300x237.jpg 300w, https:\/\/johnjohnston.info\/106\/wp-content\/uploads\/2015\/10\/5055262.jpg 1000w\" sizes=\"auto, (max-width: 300px) 85vw, 300px\" \/><\/a><\/p>\n<p>But I want to go through the images creating lots of different sets of 4 images. I don&#8217;t want to do this by hand.<\/p>\n<p>By this time I am a fair way out of my comfort zone and need to scrape my memory and google a bit. What I need is <a href=\"https:\/\/en.wikipedia.org\/wiki\/Xargs\">xargs<\/a>.<\/p>\n<p>I am still in the folder with all the 500 pixel versions of the jpgs. First I make a new folder for the montages <code>mkdir mont<\/code>.<\/p>\n<p>Then:<br \/>\n<code>ls *.jpg | xargs -n 4 sh -c 'montage -tile 2x -geometry +0+0 \"$0\" \"$@\" mont\/\"$0\" '<\/code><\/p>\n<p>The first bit: <code>ls *.jpg <\/code>, just get a list of all of the jpg files, and the pipe, |, sends it to xargs.<br \/>\nThe <em>-n 4<\/em> means that xargs uses sets of four out of the list, <em>sh<\/em> creates a command for these sets which is:<\/p>\n<p><code>'montage -tile 2x -geometry +0+0 \"$0\" \"$@\" mont\/\"$0\" '<\/code><\/p>\n<p>The <em>&#8220;$0&#8221; &#8220;$@&#8221;<\/em> just lists the set of 4 images, I did some testing with<\/p>\n<p><code>ls *.jpg | xargs -n 4 sh -c 'echo \"$0\" \"$@\" '<\/code><\/p>\n<p>To figure that out.<\/p>\n<p>I end up with a set of jpgs with a different grid of 2&#215;2 images in a folder.<\/p>\n<p><a href=\"http:\/\/johnjohnston.info\/106\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-19-at-15.01.35.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1890\" src=\"https:\/\/johnjohnston.info\/106\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-19-at-15.01.35.png\" alt=\"Screen Shot 2015-10-19 at 15.01.35\" width=\"509\" height=\"474\" srcset=\"https:\/\/johnjohnston.info\/106\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-19-at-15.01.35.png 509w, https:\/\/johnjohnston.info\/106\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-19-at-15.01.35-300x279.png 300w\" sizes=\"auto, (max-width: 509px) 85vw, 509px\" \/><\/a><\/p>\n<p>I delete the single one at the end and now\u00a0I can make a gif with sips and gifsicle, repeating the process above:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1891\" src=\"https:\/\/johnjohnston.info\/106\/wp-content\/uploads\/2015\/10\/fourby4-32colors.gif\" alt=\"fourby4-32colors\" width=\"500\" height=\"395\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>And:<\/p>\n<p><a href=\"http:\/\/johnjohnston.info\/106\/wp-content\/uploads\/2015\/10\/fencing-6x-6.gif\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"632\" class=\"aligncenter size-full wp-image-1892\" style=\"width: 100%; height: auto;\" src=\"https:\/\/johnjohnston.info\/106\/wp-content\/uploads\/2015\/10\/fencing-6x-6.gif\" alt=\"fencing-6x-6\" \/><\/a><\/p>\n<p>I tweaked the colour pallet in fireworks for this last one, getting rid of the white borders.<\/p>\n<p>There is a lot more to explore in this, I am starting to test gifsicle&#8217;s dither and colors options and will probably post a bunch of tests soonish of the results. I am also wondering if I can do some sort of zoom effect, like a movie zooming out from one fight to a battlefield&#8230;<\/p>\n<p>Update: <a href=\"http:\/\/johnjohnston.info\/oddsandends\/gifsicle\/faces\/compare.html\">Gifsicle Comparison<\/a> some tests of gifsicle parameters.<\/p>\n<div class=\"footnotes\">\n<ol>\n<li><a id=\"fn1-2015-10-18\"><\/a> <a title=\"Homebrew \u2014 The missing package manager for OS X\" href=\"http:\/\/brew.sh\">homebrew<\/a> installs the stuff you need that Apple didn\u2019t. You can get a lot of interesting command line applications using it. <a href=\"#fnr1-2015-10-18\">\u21a9<\/a><\/li>\n<li><a id=\"fn2-2015-10-19\"><\/a> <a href=\"http:\/\/www.imagemagick.org\/script\/montage.php\">ImageMagick: Command-line Tools: Montage<\/a> <em>Use the montage program to create a composite image by combining several separate images. The images are tiled on the composite image optionally adorned with a border, frame, image name, and more.<\/em> <a href=\"#fnr2-2015-10-19\">\u21a9<\/a><\/li>\n<\/ol>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>These are some notes on creating gifs and manipulating images on the command line on a mac. I am pretty much at the limit of my knowledge here so some of the details may be sketchy. It seems to me that this allows one to iterate through various ideas quite quickly. I do often take &hellip; <a href=\"https:\/\/johnjohnston.info\/106\/taking-command-of-gifs\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Taking Command of Gifs&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[74,113],"tags":[202,162,207,206],"class_list":["post-1884","post","type-post","status-publish","format-standard","hentry","category-animatedgif","category-tutorial","tag-animatedgif","tag-commandline","tag-gifsicle","tag-tutorial"],"better_featured_image":null,"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3RLlC-uo","_links":{"self":[{"href":"https:\/\/johnjohnston.info\/106\/wp-json\/wp\/v2\/posts\/1884","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/johnjohnston.info\/106\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/johnjohnston.info\/106\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/johnjohnston.info\/106\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/johnjohnston.info\/106\/wp-json\/wp\/v2\/comments?post=1884"}],"version-history":[{"count":8,"href":"https:\/\/johnjohnston.info\/106\/wp-json\/wp\/v2\/posts\/1884\/revisions"}],"predecessor-version":[{"id":1898,"href":"https:\/\/johnjohnston.info\/106\/wp-json\/wp\/v2\/posts\/1884\/revisions\/1898"}],"wp:attachment":[{"href":"https:\/\/johnjohnston.info\/106\/wp-json\/wp\/v2\/media?parent=1884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/johnjohnston.info\/106\/wp-json\/wp\/v2\/categories?post=1884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/johnjohnston.info\/106\/wp-json\/wp\/v2\/tags?post=1884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}