Quantcast
Channel: R-bloggers
Viewing all articles
Browse latest Browse all 12081

Image Processing and Manipulation with magick in R

$
0
0

(This article was first published on R Programming – DataScience+, and kindly contributed to R-bloggers)

‘ImageMagick’ is one of the famous open source libraries available for editing and manipulating Images of different types (Raster & Vector Images). magick is an R-package binding to ‘ImageMagick’ for Advanced Image-Processing in R, authored by Jeroen Ooms.

magick supports many common image formats like png, jpeg, tiff and manipulations like rotate, scale, crop, trim, blur, flip, annotate and much more.

This post is to help you get started with magick 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.

magick is available on CRAN and also on ropensci’s github.

#installing magick package from CRANinstall.packages('magick') #from Github ropensci library - note: requires RToolsdevtools::install_github('ropensci/magick')

Let us load the library and read our first image or images from the internet with image_read()

#Loading magick packagelibrary(magick)#reading a png image frink imagefrink <- image_read("https://jeroen.github.io/images/frink.png")#reading a jpg image ex-President Barack Obama from Wikipediaobama <- image_read('https://upload.wikimedia.org/wikipedia/commons/thumb/8/8d/President_Barack_Obama.jpg/800px-President_Barack_Obama.jpg')

Make sure you have got an updated version of curl package installed for the successfully reading image as mentioned above.

We can get basic details of the read images with image_info()

#image detailsimage_info(obama)image_info(frink)image_info(obama)  format width height colorspace filesize1   JPEG   800    999       sRGB   151770
image_info(frink)format width height colorspace filesize1    PNG   220    445       sRGB    73494

Communicating with RStudio, magick lets you print/display image in RStudio Viewer pane.

#displaying the imageprint(obama)

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 magick‘s image_write().

#Rendering JPG image into SVGimage_write(obama, path = 'obama.svg', format = 'svg')

Below is the PNG format of Obama Image that we read in from Wikipedia:

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 magick supports pipe %>% operator.

This is what we are going to do:

    1. Take the Original Obama Image and Apply Charcoal Effect to it 2. Composite Frink Image along with it 3. Annotate some text – considering Obama would’ve dealt with Confidential data – Let it be CONFIDENTIAL stamped on the image 4. Finally, Rotate the image slightly and write/save it.
#Applying Charcoal effect to Obama's image #and compositing it with frink's image#and finally annotating it with a textimage_charcoal(obama) %>%   image_composite(frink)  %>%  image_annotate("CONFIDENTIAL", size = 50, color = "red", boxcolor = "pink",                 degrees = 30, location = "+100+100") %>%  image_rotate(30) %>%  image_write('obama_with_frink.png','png')

Gives this output:

How does the output image look? Artistic isn’t it ;)!

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 magick‘s image_annotate()

library(ggplot2)img <- image_graph(600, 400, res = 96)p <- ggplot(iris) + geom_point(aes(Sepal.Length,Petal.Length))print(p)dev.off()img %>% image_annotate("CONFIDENTIAL", size = 50, color = "red", boxcolor = "pink",                       degrees = 30, location = "+100+100")  %>%  image_write('conf_ggplot.png','png')

Gives this output image:

This is nothing but a glimpse of what magick can do. Literally, there is a lot of magic left in magick. Try it out yourself and share your thoughts in comments. The code used here can be found on my github.

References:

    Related Post

    1. Analyzing the Bible and the Quran using Spark
    2. Predict Customer Churn – Logistic Regression, Decision Tree and Random Forest
    3. Find Your Best Customers with Customer Segmentation in Python
    4. Principal Component Analysis – Unsupervised Learning
    5. ARIMA models and Intervention Analysis
    var vglnk = { key: '949efb41171ac6ec1bf7f206d57e90b8' }; (function(d, t) {var s = d.createElement(t); s.type = 'text/javascript'; s.async = true;s.src = '//cdn.viglink.com/api/vglnk.js';var r = d.getElementsByTagName(t)[0]; r.parentNode.insertBefore(s, r); }(document, 'script'));

    To leave a comment for the author, please follow the link and comment on their blog: R Programming – DataScience+.

    R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...


    Viewing all articles
    Browse latest Browse all 12081

    Trending Articles



    <script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>