<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/image-processing-and-manipulation-with-magick-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://datascienceplus.com/image-processing-and-manipulation-with-magick-in-r/"> R Programming – DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><strong>‘ImageMagick’</strong> is one of the famous open source libraries available for editing and manipulating Images of different types (Raster &amp; Vector Images). <strong><code><a rel="nofollow" href="https://github.com/ropensci/magick" target="_blank">magick</a></code></strong> is an R-package binding to ‘ImageMagick’ for Advanced Image-Processing in R, authored by Jeroen Ooms.</p>
<p><strong>magick</strong> supports many common image formats like png, jpeg, tiff and manipulations like rotate, scale, crop, trim, blur, flip, annotate and much more.</p>
<p>This post is to help you get started with <code>magick</code> to process, edit and manipulate images in R that could be anything from just file format conversion (eg. png to jpeg) or annotating R graphics output. </p>
<p><code>magick</code> is available on <a rel="nofollow" href="https://cran.r-project.org/web/packages/magick/magick.pdf" target="_blank">CRAN</a> and also on <a rel="nofollow" href="https://github.com/ropensci/magick" target="_blank">ropensci’s github</a>. </p>
<pre>
#installing magick package from CRAN
install.packages('magick') 
#from Github ropensci library - note: requires RTools
devtools::install_github('ropensci/magick')
</pre>
<p>Let us load the library and read our first image or images from the internet with <code>image_read()</code></p>
<pre>
#Loading magick package
library(magick)

#reading a png image frink image
frink &lt;- image_read(&quot;https://jeroen.github.io/images/frink.png&quot;)
#reading a jpg image ex-President Barack Obama from Wikipedia
obama &lt;- image_read('https://upload.wikimedia.org/wikipedia/commons/thumb/8/8d/President_Barack_Obama.jpg/800px-President_Barack_Obama.jpg')
</pre>
<p>Make sure you have got an updated version of <code>curl</code> package installed for the successfully reading image as mentioned above. </p>
<p>We can get basic details of the read images with <code>image_info()</code></p>
<pre>
#image details
image_info(obama)
image_info(frink)
<em>image_info(obama)
format width height colorspace filesize
1 JPEG 800 999 sRGB 151770</em></pre>
<pre>
image_info(frink)
<em>format width height colorspace filesize
1 PNG 220 445 sRGB 73494
</em></pre>
<p>Communicating with RStudio, <code>magick</code> lets you print/display image in RStudio Viewer pane. </p>
<pre>
#displaying the image
print(obama)
</pre>
<p>Whether you are a web developer or you are putting together a powerpoint deck, Image File Format Conversion is one of the operations that we would end up doing and this is literally an one-liner using <code>magick</code>‘s <code>image_write()</code>.</p>
<pre>
#Rendering JPG image into SVG
image_write(obama, path = 'obama.svg', format = 'svg')
</pre>
<p>Below is the PNG format of Obama Image that we read in from Wikipedia:<br>
<a rel="nofollow" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2017/11/obama.png?ssl=1" target="_blank"><img sizes="(max-width: 392px) 100vw, 392px" src="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2017/11/obama-392x490.png?resize=392%2C490&ssl=1" alt="" class="wp-image-9583" data-recalc-dims="1"></a></p>
<p>But you might be wondering, “Hey! This is just basic Image processing. Didn’t you tell this is advanced?” And Yes, Here comes the advanced stuff along with a good news that <code>magick</code> supports pipe <code>%>%</code> operator. </p>
<p>This is what we are going to do:</p>
<ul>
1. Take the Original Obama Image and Apply Charcoal Effect to it<br>
2. Composite Frink Image along with it<br>
3. Annotate some text – considering Obama would’ve dealt with Confidential data – Let it be CONFIDENTIAL stamped on the image<br>
4. Finally, Rotate the image slightly and write/save it. </ul>
<pre>
#Applying Charcoal effect to Obama's image 
#and compositing it with frink's image
#and finally annotating it with a text
image_charcoal(obama) %>% 
  image_composite(frink)  %>%
  image_annotate(&quot;CONFIDENTIAL&quot;, size = 50, color = &quot;red&quot;, boxcolor = &quot;pink&quot;,
                 degrees = 30, location = &quot;+100+100&quot;) %>%
  image_rotate(30) %>%
  image_write('obama_with_frink.png','png')
</pre>
<p>Gives this output:<br>
<a rel="nofollow" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2017/11/obama_with_frink.png?ssl=1" target="_blank"><img sizes="(max-width: 462px) 100vw, 462px" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2017/11/obama_with_frink-462x490.png?w=450&ssl=1" alt="" class="wp-image-9584" data-recalc-dims="1"></a></p>
<p>How does the output image look? Artistic isn’t it ;)!</p>
<p>But this is not Data Scientists want to do every day, instead, we play with Plots and most of the time want to annotate the R Graphics output and here’s how you can do that with <code>magick</code>‘s <code>image_annotate()</code></p>
<pre>
library(ggplot2)
img &lt;- image_graph(600, 400, res = 96)
p &lt;- ggplot(iris) + geom_point(aes(Sepal.Length,Petal.Length))
print(p)
dev.off()
img %>% image_annotate(&quot;CONFIDENTIAL&quot;, size = 50, color = &quot;red&quot;, boxcolor = &quot;pink&quot;,
                       degrees = 30, location = &quot;+100+100&quot;)  %>%
  image_write('conf_ggplot.png','png')
</pre>
<p>Gives this output image:<br>
<a rel="nofollow" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2017/11/conf_ggplot.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2017/11/conf_ggplot-490x327.png?w=450&ssl=1" alt="" class="wp-image-9585" data-recalc-dims="1"></a></p>
<p>This is nothing but a glimpse of what <code>magick</code> can do. Literally, there is a lot of <i>magic</i> left in magick. Try it out yourself and share your thoughts in comments. The code used here can be found on <a rel="nofollow" href="https://github.com/amrrs/datascienceplus_codes/blob/master/magick_r.R" target="_blank">my github</a>. </p>
<p>References:</p>
<ul>
1. <a rel="nofollow" href="https://github.com/ropensci/magick" target="_blank">Magick Ropensci</a><br>
2. <a rel="nofollow" href="https://cran.r-project.org/web/packages/magick/magick.pdf" target="_blank">Magick CRAN Documentation</a><br>
3. <a rel="nofollow" href="https://en.wikipedia.org/wiki/Barack_Obama#/media/File:President_Barack_Obama.jpg" target="_blank">Obama Image Credit</a><br>
4. <a rel="nofollow" href="https://jeroen.github.io/images/frink.png" target="_blank">Frink Image Credit</a>
</ul>
<ul></ul>
<p><strong>
<p>Related Post</p>
<p></p></strong>
<ol>
<li><a rel="nofollow" href="https://datascienceplus.com/analyzing-the-bible-and-the-quran-using-spark/" title="Permanent Link toAnalyzing the Bible and the Quran using Spark" target="_blank">Analyzing the Bible and the Quran using Spark</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/predict-customer-churn-logistic-regression-decision-tree-and-random-forest/" title="Permanent Link toPredict Customer Churn – Logistic Regression, Decision Tree and Random Forest" target="_blank">Predict Customer Churn – Logistic Regression, Decision Tree and Random Forest</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/find-your-best-customers-with-customer-segmentation-in-python/" title="Permanent Link toFind Your Best Customers with Customer Segmentation in Python" target="_blank">Find Your Best Customers with Customer Segmentation in Python</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/principal-component-analysis-unsupervised-learning/" title="Permanent Link toPrincipal Component Analysis – Unsupervised Learning" target="_blank">Principal Component Analysis – Unsupervised Learning</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/arima-models-and-intervention-analysis/" title="Permanent Link toARIMA models and Intervention Analysis" target="_blank">ARIMA models and Intervention Analysis</a></li>
</ol>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://datascienceplus.com/image-processing-and-manipulation-with-magick-in-r/"> R Programming – DataScience+</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/GiMCCnREnn8" height="1"></a></div>