<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/adding-bananas-from-the-commandline-extending-the-oomsifier/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://rmhogervorst.nl/cleancode/blog/2017/12/29/extending-the-oomsifyer-cmdline.html"> Clean Code</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Sometimes you just want to add bananas from the commandline. <a rel="nofollow" href="http://rmhogervorst.nl/cleancode/blog/2017/11/28/building-the-oomsifier.html" target="_blank">Previously</a><br>
I created a small script that takes an image and adds a dancing banana to the bottom left of the image. I wanted to make an API too, but that will have to wait till next year. Today we will create a commandline script that will do the same thing.</p>
<p>With the excellent explanation in <a rel="nofollow" href="http://blog.sellorm.com/2017/12/18/learn-to-write-command-line-utilities-in-r/" target="_blank">Mark Sellors’ guide</a> I have now created a cmdline thingy in very few steps.</p>
<p><img alt="" src="https://i0.wp.com/rmhogervorst.nl/cleancode/images/r-pkg.gif?w=456" data-recalc-dims="1"></p>
<p>I can now add bananas from the commandline with:</p>
<div>
<div>
<pre><code>./bananafy.R ../images/ggplotexample.png out.gif
</code></pre>
</div>
</div>
<p>This executes and says:</p>
<div>
<div>
<pre><code>Linking to ImageMagick 6.9.7.4
Enabled features: fontconfig, freetype, fftw, lcms, pango, x11
Disabled features: cairo, ghostscript, rsvg, webp
writing bananafied image to out.gif
</code></pre>
</div>
</div>
<h4>The modified script</h4>
<p>First the script itself, saved as bananafy.R</p>
<div>
<div>
<pre><code><span>#!/usr/bin/Rscript --vanilla</span><span>
</span><span>args</span><span> </span><span>&lt;-</span><span> </span><span>commandArgs</span><span>(</span><span>trailingOnly</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>if</span><span> </span><span>(</span><span>length</span><span>(</span><span>args</span><span>)</span><span> </span><span></span><span> </span><span>1</span><span>){</span><span>
</span><span>stop</span><span>(</span><span>&quot;I think you forgot to input an image and output name? \n&quot;</span><span>)</span><span>
</span><span>}</span><span>
</span><span>library</span><span>(</span><span>magick</span><span>)</span><span>
</span><span>## Commandline version of add banana</span><span>
</span><span>#banana &lt;-image_read(&quot;images/banana.gif&quot;) # this assumes you have a project with the folder /images/ inside.</span><span>
</span><span>#add_banana &lt;-function(, offset = NULL, debug = FALSE){</span><span>
</span><span>offset</span><span> </span><span>&lt;-</span><span> </span><span>NULL</span><span> </span><span># maybe a third argument here would be cool?</span><span>
</span><span>debug</span><span> </span><span>&lt;-</span><span> </span><span>FALSE</span><span>
</span><span>image_in</span><span> </span><span>&lt;-</span><span> </span><span>magick</span><span>::</span><span>image_read</span><span>(</span><span>args</span><span>[[</span><span>1</span><span>]])</span><span>
</span><span>banana</span><span> </span><span>&lt;-</span><span> </span><span>image_read</span><span>(</span><span>&quot;../images/banana.gif&quot;</span><span>)</span><span> </span><span># 365w 360 h</span><span>
</span><span>image_info</span><span> </span><span>&lt;-</span><span> </span><span>image_info</span><span>(</span><span>image_in</span><span>)</span><span>
</span><span>if</span><span>(</span><span>&quot;gif&quot;</span><span> </span><span>%in%</span><span> </span><span>image_info</span><span>$</span><span>format</span><span> </span><span>){</span><span>stop</span><span>(</span><span>&quot;gifs are to difficult for me now&quot;</span><span>)}</span><span>
</span><span>stopifnot</span><span>(</span><span>nrow</span><span>(</span><span>image_info</span><span>)</span><span>==</span><span>1</span><span>)</span><span>
</span><span># scale banana to correct size:</span><span>
</span><span># take smallest dimension.</span><span>
</span><span>target_height</span><span> </span><span>&lt;-</span><span> </span><span>min</span><span>(</span><span>image_info</span><span>$</span><span>width</span><span>,</span><span> </span><span>image_info</span><span>$</span><span>height</span><span>)</span><span>
</span><span># scale banana to 1/3 of the size</span><span>
</span><span>scaling</span><span> </span><span>&lt;-</span><span> </span><span>(</span><span>target_height</span><span> </span><span>/</span><span>3</span><span>)</span><span>
</span><span>front</span><span> </span><span>&lt;-</span><span> </span><span>image_scale</span><span>(</span><span>banana</span><span>,</span><span> </span><span>scaling</span><span>)</span><span>
</span><span># place in lower right corner</span><span>
</span><span># offset is width and hieight minus dimensions picutre?</span><span>
</span><span>scaled_dims</span><span> </span><span>&lt;-</span><span> </span><span>image_info</span><span>(</span><span>front</span><span>)</span><span>
</span><span>x_c</span><span> </span><span>&lt;-</span><span> </span><span>image_info</span><span>$</span><span>width</span><span> </span><span>-</span><span> </span><span>scaled_dims</span><span>$</span><span>width</span><span>
</span><span>y_c</span><span> </span><span>&lt;-</span><span> </span><span>image_info</span><span>$</span><span>height</span><span> </span><span>-</span><span> </span><span>scaled_dims</span><span>$</span><span>height</span><span>
</span><span>offset_value</span><span> </span><span>&lt;-</span><span> </span><span>ifelse</span><span>(</span><span>is.null</span><span>(</span><span>offset</span><span>),</span><span> </span><span>paste0</span><span>(</span><span>&quot;+&quot;</span><span>,</span><span>x_c</span><span>,</span><span>&quot;+&quot;</span><span>,</span><span>y_c</span><span>),</span><span> </span><span>offset</span><span>)</span><span>
</span><span>if</span><span>(</span><span>debug</span><span>)</span><span> </span><span>print</span><span>(</span><span>offset_value</span><span>)</span><span>
</span><span>frames</span><span> </span><span>&lt;-</span><span> </span><span>lapply</span><span>(</span><span>as.list</span><span>(</span><span>front</span><span>),</span><span> </span><span>function</span><span>(</span><span>x</span><span>)</span><span> </span><span>image_composite</span><span>(</span><span>image_in</span><span>,</span><span> </span><span>x</span><span>,</span><span> </span><span>offset</span><span> </span><span>=</span><span> </span><span>offset_value</span><span>))</span><span>
</span><span>result</span><span> </span><span>&lt;-</span><span> </span><span>image_animate</span><span>(</span><span>image_join</span><span>(</span><span>frames</span><span>),</span><span> </span><span>fps</span><span> </span><span>=</span><span> </span><span>10</span><span>)</span><span>
</span><span>message</span><span>(</span><span>&quot;writing bananafied image to&quot;</span><span>,</span><span> </span><span>args</span><span>[[</span><span>2</span><span>]])</span><span>
</span><span>image_write</span><span>(</span><span>image</span><span> </span><span>=</span><span> </span><span>result</span><span>,</span><span> </span><span>path</span><span> </span><span>=</span><span> </span><span>args</span><span>[[</span><span>2</span><span>]])</span><span>
</span></code></pre>
</div>
</div>
<p>As you might notice I copied the entire thing from the previous post and added some extra Things</p>
<ul>
<li>It starts with ‘#!/usr/bin/Rscript’</li>
</ul>
<p>According to Mark:</p>
<blockquote>
<p>Sometimes called a ‘shebang’, this line tells the Linux and MacOS command line interpreters (which both default to one called ‘bash’), what you want to use to run the rest of the code in the file. ….The –vanilla on the end, tells Rscript to run without saving or restoring anything in the process. This just keeps things nice a clean.</p>
</blockquote>
<p>I’ve added a message call that tells me where the script saves the image. I could have suppressed the magic messages, but meh, it is a proof of concept.</p>
<p>To make it work, you have to tell linux (which I’m working on) that it can execute the file. That means changing the permissions on that file</p>
<p>In the terminal you go to the projectfolder and type <code>chmod +x bananafy.R</code>. You CHange MODe by adding (+) eXecution rights to that file.</p>
<h3>advanced use: making bananafy options available always and everywhere in the terminal.</h3>
<p>We could make the bananafyer available to you always in in every folder. T do that you could move the script to f.i. ~/scripts/, modify the code a bit and add the bananagif to that same folder. You then have to <a rel="nofollow" href="https://askubuntu.com/questions/153251/launch-shell-scripts-from-anywhere" target="_blank">modify your bashrc file</a>.</p>
<ul>
<li>I had to make the link to the banana hardcoded: ‘~/scripts/images/banana.gif’</li>
<li>you can call the code from anywhere and the output of the script will end up in the folder you currently are in. So if I’m in ~/pictures/reallynicepictures the bananafied image will be there.</li>
</ul>
<p><a rel="nofollow" href="http://rmhogervorst.nl/cleancode/blog/2017/12/29/extending-the-oomsifyer-cmdline.html" target="_blank">Adding bananas from the commandline (extending the oomsifier)</a> was originally published by at <a rel="nofollow" href="http://rmhogervorst.nl/cleancode" target="_blank">Clean Code</a> on December 29, 2017.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://rmhogervorst.nl/cleancode/blog/2017/12/29/extending-the-oomsifyer-cmdline.html"> Clean Code</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/yqOw8GOdRgk" height="1"></a></div>