<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/magick-1-0-%f0%9f%8e%a9-%e2%9c%a8%f0%9f%90%87-advanced-graphics-and-image-processing-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ropensci.org/blog/blog/2017/08/15/magick-10"> rOpenSci Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Last week, version 1.0 of the <a rel="nofollow" href="https://cran.r-project.org/web/packages/magick/index.html" target="_blank">magick</a> package appeared on CRAN: an ambitious effort to modernize and simplify high quality image processing in R. This R package builds upon the <a rel="nofollow" href="https://www.imagemagick.org/Magick++/STL.html" target="_blank">Magick++ STL</a> which exposes a powerful C++ API to the famous ImageMagick library.</p>
<p><img alt="RStudio Screenshot" src="https://i1.wp.com/jeroen.github.io/images/magick.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>The best place to start learning about magick is the <a rel="nofollow" href="https://cran.r-project.org/web/packages/magick/vignettes/intro.html" target="_blank">vignette</a> which gives a brief overview of the overwhelming amount of functionality in this package.</p>
<h2>Towards Release 1.0</h2>
<p>Last year around this time rOpenSci <a rel="nofollow" href="https://ropensci.org/blog/blog/2016/08/23/z-magick-release" target="_blank">announced</a> the first release of the magick package: a new powerful toolkit for image reading, writing, converting, editing, transformation, annotation, and animation in R. Since the initial release there have been several updates with additional functionality, and many useRs have started to discover the power of this package to take visualization in R to the next level.</p>
<p>For example <a rel="nofollow" href="https://twitter.com/hrbrmstr/status/758304420224466944" target="_blank">Bob Rudis</a> uses magick to visualize California drought data from the U.S. Drought Monitor (click on the image to go find out more):</p>
<p><a rel="nofollow" href="https://rud.is/b/2016/07/27/u-s-drought-animations-with-the-witchs-brew-purrr-broom-magick/" target="_blank"><img alt="drought" src="https://i0.wp.com/jeroen.github.io/images/drought.gif?w=456&ssl=1" data-recalc-dims="1"></a></p>
<p>R-ladies <a rel="nofollow" href="http://www.lucymcgowan.com/" target="_blank">Lucy D'Agostino McGowan</a> and <a rel="nofollow" href="http://www.masalmon.eu/" target="_blank">Maëlle Salmon</a> demonstrate how to make a beautiful collage:</p>
<p><a rel="nofollow" href="http://livefreeordichotomize.com/2017/07/18/the-making-of-we-r-ladies/" target="_blank"><img alt="collage" src="https://i0.wp.com/livefreeordichotomize.com/images/we-r-ladies.jpeg?w=456" data-recalc-dims="1"></a></p>
<p>And <a rel="nofollow" href="https://twitter.com/danielphadley/status/884845188979359744" target="_blank">Daniel P. Hadley</a> lets Vincent Vega explains Cars:</p>
<p><a rel="nofollow" href="http://danielphadley.com/ggplot-Logo/" target="_blank"><img alt="travolta" src="https://i1.wp.com/danielphadley.com/images/Cars_Travolta.gif?w=456" data-recalc-dims="1"></a></p>
<p>Now, 1 year later, the 1.0 release marks an important milestone: the addition of a new native graphics device (which serves as a hybrid between a magick image object and an R plot) bridges the gap between graphics and image processing in R.</p>
<p>This blog post explains how the magick device allows you to seamlessly combine graphing with image processing in R. You can either use it to post-process your R graphics, or draw on imported images using the native R plotting machinery. We hope that this unified interface will make it easier to produce beautiful, reproducible images with R.</p>
<h2>Native Magick Graphics</h2>
<p>The <code>image_graph()</code> function opens a new graphics device similar to e.g. <code>png()</code> or <code>x11()</code>. It returns an image object to which the plot(s) will be written. Each page in the plotting device will become a frame (layer) in the image object.</p>
<div>
<pre><code data-lang="r"><span></span><span># Produce image using graphics device</span>
fig <span>&lt;-</span> image_graph<span>(</span>res <span>=</span> <span>96</span><span>)</span>
ggplot2<span>::</span>qplot<span>(</span>mpg<span>,</span> wt<span>,</span> data <span>=</span> mtcars<span>,</span> colour <span>=</span> cyl<span>)</span>
dev.off<span>()</span>
</code></pre>
</div>
<p>The <code>fig</code> object now contains the image that we can easily post-process. For example we can overlay another image:</p>
<div>
<pre><code data-lang="r"><span></span>logo <span>&lt;-</span> image_read<span>(</span><span>&quot;https://www.r-project.org/logo/Rlogo.png&quot;</span><span>)</span>
out <span>&lt;-</span> image_composite<span>(</span>fig<span>,</span> image_scale<span>(</span>logo<span>,</span> <span>&quot;x150&quot;</span><span>),</span> offset <span>=</span> <span>&quot;+80+380&quot;</span><span>)</span>

<span># Show preview</span>
image_browse<span>(</span>out<span>)</span>

<span># Write to file</span>
image_write<span>(</span>out<span>,</span> <span>&quot;myplot.png&quot;</span><span>)</span>
</code></pre>
</div>
<p><img alt="out" src="https://i1.wp.com/ropensci.org/assets/blog-images/2017-08-15-magick-10/out.png?w=456" data-recalc-dims="1"></p>
<h2>Drawing Device</h2>
<p>The <code>image_draw()</code> function opens a graphics device to draw on top of an existing image using pixel coordinates.</p>
<div>
<pre><code data-lang="r"><span></span><span># Open a file</span>
<span>library</span><span>(</span>magick<span>)</span>
frink <span>&lt;-</span> image_read<span>(</span><span>&quot;https://jeroen.github.io/images/frink.png&quot;</span><span>)</span>
drawing <span>&lt;-</span> image_draw<span>(</span>frink<span>)</span>
</code></pre>
</div>
<p><img alt="frink" src="https://i1.wp.com/jeroen.github.io/images/frink.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>We can now use R's native low-level graphics functions for drawing on top of the image:</p>
<div>
<pre><code data-lang="r"><span></span>rect<span>(</span><span>20</span><span>,</span> <span>20</span><span>,</span> <span>200</span><span>,</span> <span>100</span><span>,</span> border <span>=</span> <span>&quot;red&quot;</span><span>,</span> lty <span>=</span> <span>&quot;dashed&quot;</span><span>,</span> lwd <span>=</span> <span>5</span><span>)</span>
abline<span>(</span>h <span>=</span> <span>300</span><span>,</span> col <span>=</span> <span>'blue'</span><span>,</span> lwd <span>=</span> <span>'10'</span><span>,</span> lty <span>=</span> <span>&quot;dotted&quot;</span><span>)</span>
text<span>(</span><span>10</span><span>,</span> <span>250</span><span>,</span> <span>&quot;Hoiven-Glaven&quot;</span><span>,</span> family <span>=</span> <span>&quot;courier&quot;</span><span>,</span> cex <span>=</span> <span>4</span><span>,</span> srt <span>=</span> <span>90</span><span>)</span>
palette<span>(</span>rainbow<span>(</span><span>11</span><span>,</span> end <span>=</span> <span>0.9</span><span>))</span>
symbols<span>(</span><span>rep</span><span>(</span><span>200</span><span>,</span> <span>11</span><span>),</span> <span>seq</span><span>(</span><span>0</span><span>,</span> <span>400</span><span>,</span> <span>40</span><span>),</span> circles <span>=</span> runif<span>(</span><span>11</span><span>,</span> <span>5</span><span>,</span> <span>35</span><span>),</span>
  bg <span>=</span> <span>1</span><span>:</span><span>11</span><span>,</span> inches <span>=</span> <span>FALSE</span><span>,</span> add <span>=</span> <span>TRUE</span><span>)</span>
</code></pre>
</div>
<p>At any point you can inspect the current result:</p>
<div>
<pre><code data-lang="text"><span></span>image_browse(drawing)
</code></pre>
</div>
<p><img alt="drawing" src="https://i2.wp.com/ropensci.org/assets/blog-images/2017-08-15-magick-10/drawing.png?w=456" data-recalc-dims="1"></p>
<p>Once you are done you can close the device and save the result.</p>
<div>
<pre><code data-lang="r"><span></span>dev.off<span>()</span>
image_write<span>(</span>drawing<span>,</span> <span>'drawing.png'</span><span>)</span>
</code></pre>
</div>
<p>By default <code>image_draw()</code> sets all margins to 0 and uses graphics coordinates to match image size in pixels (width x height) where (0,0) is the top left corner. Note that this means the y axis increases from top to bottom which is the opposite of typical graphics coordinates. You can override all this by passing custom <code>xlim</code>, <code>ylim</code> or <code>mar</code> values to <code>image_draw()</code>.</p>
<h2>Animated Graphics</h2>
<p>The graphics device supports multiple frames which makes it easy to create animated graphics. The example below shows how you would implement the example from the very cool <a rel="nofollow" href="https://github.com/dgrtwo/gganimate" target="_blank">gganimate</a> package using the magick.</p>
<div>
<pre><code data-lang="r"><span></span><span>library</span><span>(</span>gapminder<span>)</span>
<span>library</span><span>(</span>ggplot2<span>)</span>
<span>library</span><span>(</span>magick<span>)</span>
img <span>&lt;-</span> image_graph<span>(</span>res <span>=</span> <span>96</span><span>)</span>
datalist <span>&lt;-</span> <span>split</span><span>(</span>gapminder<span>,</span> gapminder<span>$</span>year<span>)</span>
out <span>&lt;-</span> <span>lapply</span><span>(</span>datalist<span>,</span> <span>function</span><span>(</span>data<span>){</span>
  p <span>&lt;-</span> ggplot<span>(</span>data<span>,</span> aes<span>(</span>gdpPercap<span>,</span> lifeExp<span>,</span> size <span>=</span> pop<span>,</span> color <span>=</span> continent<span>))</span> <span>+</span>
    scale_size<span>(</span><span>&quot;population&quot;</span><span>,</span> limits <span>=</span> <span>range</span><span>(</span>gapminder<span>$</span>pop<span>))</span> <span>+</span>
    scale_x_log10<span>(</span>limits <span>=</span> <span>range</span><span>(</span>gapminder<span>$</span>gdpPercap<span>))</span> <span>+</span>
    geom_point<span>()</span> <span>+</span> ylim<span>(</span><span>20</span><span>,</span> <span>90</span><span>)</span> <span>+</span>  ggtitle<span>(</span>data<span>$</span>year<span>)</span> <span>+</span> theme_classic<span>()</span>
  <span>print</span><span>(</span>p<span>)</span>
<span>})</span>
dev.off<span>()</span>
animation <span>&lt;-</span> image_animate<span>(</span>img<span>,</span> fps <span>=</span> <span>2</span><span>)</span>
image_write<span>(</span>animation<span>,</span> <span>&quot;animation.gif&quot;</span><span>)</span>
</code></pre>
</div>
<p><img alt="animation" src="https://i2.wp.com/ropensci.org/assets/blog-images/2017-08-15-magick-10/animation.gif?w=456" data-recalc-dims="1"></p>
<p>We hope that the magick package can provide a more robust back-end for packages like gganimate to produce interactive graphics in R without requiring the user to manually install external image editing software.</p>
<h2>Porting ImageMagick Commands to R</h2>
<p>The magick 1.0 release now has the core image processing functionality that you expect from an image processing package. But there is still a lot of room for improvement to make magick <em>the</em> image processing package in R.</p>
<p>A lot of R users and packages currently shell out to ImageMagick command line tools for performing image manipulations. The goal is to support all these operations in the magick package, so that the images can be produced (and reproduced!) on any platform without requiring the user to install additional software.</p>
<p>Note that ImageMagick library is over 26 years old and has accumulated an enormous number of features in those years. Porting all of this to R is quite a bit of work, for which feedback from users is important. If there is an imagemagick operation that you like to do in R but you can't figure out how, please <a rel="nofollow" href="https://github.com/ropensci/magick/issues" target="_blank">open an issue</a> on GitHub. If the functionality is currently not supported yet, we will try to add it to the next version.</p>
<h2>Image Analysis</h2>
<p>Currently magick is focused on generating and editing images. There is yet another entirely different set of features which we like to support related to analyzing images. Image analysis can involve anything from calculating color distributions to more sophisticated feature extraction and vision tools. I am not very familiar with this field, so again we could use suggestions from users and experts.</p>
<p>One feature that is already available is the <code>image_ocr()</code> function which extracts text from the image using the rOpenSci <a rel="nofollow" href="https://ropensci.org/blog/blog/2016/11/16/tesseract" target="_blank">tesseract</a> package. Another cool example of using image analysis is the <a rel="nofollow" href="https://github.com/ThinkRstat/collage" target="_blank">collage</a> package which calculates <a rel="nofollow" href="https://github.com/ThinkRstat/collage#histograms" target="_blank">color histograms</a> to select appropriate tile images for creating a collage.</p>
<p><a rel="nofollow" href="https://github.com/ThinkRstat/collage#histograms" target="_blank"><img alt="histogram" src="https://i1.wp.com/github.com/ThinkRstat/collage/raw/master/README-histograms-2.png?w=456&ssl=1" data-recalc-dims="1"></a></p>
<p>As part of supporting supporting analysis tools we plan to extract the bitmap (raster) classes into a separate package. This will enable package authors to write R extensions to analyze and manipulate on the raw image data, without necessarily depending on magick. Yet the user can always rely on magick as a powerful toolkit to import/export images and graphics into such low level bitmaps.</p>
<div>
<blockquote class="twitter-tweet" data-lang="en"><p>
<a rel="nofollow" href="https://twitter.com/grbails/status/885543687559811073" target="_blank"></a>
</p></blockquote>
<p>
</div>
	<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://ropensci.org/blog/blog/2017/08/15/magick-10"> rOpenSci Blog</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/FGVjIgmtA4I" height="1"></a></div>