Recently, there has been a huge rise in the implementation of artificial intelligence solutions, with new deep learning architectures being built and deployed across various industries. This rise could be attributed to two important factors:

Availability of Computational Power (eg: GPUs, AWS / Digital Ocean Instances)

Availability of Training Datasets (eg: MNIST, ImageNet, Fashion MNIST)

Deep learning works primarily because of the vast amount of input data on which the deep neural net is trained. Hence, having a good labeled training dataset marks the first step in developing a highly accurate AI solution.

Preparing labeled training datasets for computer vision problems is a painstaking task that involves image processing, manipulation, and finally image labeling. Thus, while dealing with hundreds and thousands of images, programmatic image manipulation and processing stands out as an efficient option for AI dataset creators. So getting familiar with image processing libraries is a convenient first step in creating a custom AI Solution.

ImageMagick

ImageMagick is one such tool, and in fact, it’s one of the most comprehensive open-source image processing libraries. It supports more than 200 image file formats (like: png, jpeg, tiff, pdf) and can display, convert, and edit raster image and vector image files. Jeroen Ooms has been kind enough to develop an R package, magick , that wraps the ImageMagick ST library. Thus, the R-package magick can help R users with advanced image processing.

Installation on Windows and OSX

Since magick is available on CRAN, installing magick is as straightforward as installing any other R-package with install.packages()

install.packages('magick')

Also, the development version of magick is available on ropensci 's GitHub repo and can be installed with install_github() of devtools package.

devtools::install_github("ropensci/magick")

Please note that for installing from the source (the development version from GitHub), the destination workstation requires RTools.exe to build the source. Also note that the binary CRAN packages work out of the box and have the most important features enabled, which makes installing from CRAN the preferable option.

Installation on Linux

On Linux you need to install the ImageMagick++ library: on Debian/Ubuntu this is called libmagick++-dev:

sudo apt-get install libmagick++-dev

On Fedora or CentOS/RHEL we need ImageMagick-c++-devel:

sudo yum install ImageMagick-c++-devel

Once the installation is successful, the library magick can be loaded into the current R session using library() function.

library(magick)

Reading, Viewing, and Writing Image files

magick supports reading image files from both the local computer or a url on the Internet using the image_read() function.

#from URL

#inp_img <- image_read("http://www.catster.com/wp-content/uploads/2017/08/A-fluffy-cat-looking-funny-surprised-or-concerned.jpg") #inp_img http://cdn.coda-craven.org/wp-content/uploads/2016/10/little-cute-cat-wallpaper-coda-craven-680x425.jpg ") #from Local Computer

inp_img <- image_read('../input/catjpg/cat.jpg')

Once the image is read into R, it’s basic information, such as image format, dimension, and file size can be extracted using the image_info() function:

image_info(inp_img) #Output #format width height colorspace matte filesize #JPEG 900 900 sRGB FALSE 156921

If you’re using RStudio (or any other editor with a built-in browser), the read image ( inp_img ) is automatically displayed in the Viewer after every execution or simply when we mention the image object name ( inp_img ). In other cases, to view the image, functions like plot() or print() should be wrapped around the magick object to display the image.

plot(inp_img)

Also, the same magick object could be viewed in the default photoviewer application of your system using image_browse()

image_browse(inp_img)

Similar to image_read() for reading image files into R, image_write() is the function that helps in writing/saving image files in the desired format. The first argument of image_write() is the magick image object into which the image_read() output was saved — in our case inp_img — and the second argument is the path along with the file name where the image has be written/saved. The third argument is the image format in which the image has to be written/saved — like png or svg .

While image_write() can be used for image format conversion — like from .jpeg to .png — magick has image_convert() , which also performs the same image conversion but saves the image object in the memory (the current session) unlike image_write() , which writes/saves the image object to an external file.

#converting the existing jpeg format image into png

inp_img_png <- image_convert(inp_img, format = 'png')

As the above output suggests, image_convert() has successfully converted the input jpeg formatted image object into png format but has saved it in memory (in a new R object inp_img_png ). And to write the same to an external file, we can use image_write()