The magick package provide a modern and simple toolkit for image processing in R. It wraps the ImageMagick STL which is the most comprehensive open-source image processing library available today.
The ImageMagick library has an overwhelming amount of functionality. Magick exposes a decent subset of it, but it is impossible to document everything in detail. This article introduces some basic concepts and examples to get started.
magick
On Windows or macOS the package is most easily installed via CRAN.
install.packages("magick")
The binary CRAN packages work out of the box and have most important
features enabled. Use magick_config
to see which features
and formats are supported by your version of ImageMagick.
library(magick)
## Linking to ImageMagick 6.9.12.93
## Enabled features: cairo, fontconfig, freetype, heic, lcms, pango, raw, rsvg, webp
## Disabled features: fftw, ghostscript, x11
str(magick::magick_config())
## List of 24
## $ version :Class 'numeric_version' hidden list of 1
## ..$ : int [1:4] 6 9 12 93
## $ modules : logi FALSE
## $ cairo : logi TRUE
## $ fontconfig : logi TRUE
## $ freetype : logi TRUE
## $ fftw : logi FALSE
## $ ghostscript : logi FALSE
## $ heic : logi TRUE
## $ jpeg : logi TRUE
## $ lcms : logi TRUE
## $ libopenjp2 : logi TRUE
## $ lzma : logi TRUE
## $ pangocairo : logi TRUE
## $ pango : logi TRUE
## $ png : logi TRUE
## $ raw : logi TRUE
## $ rsvg : logi TRUE
## $ tiff : logi TRUE
## $ webp : logi TRUE
## $ wmf : logi FALSE
## $ x11 : logi FALSE
## $ xml : logi TRUE
## $ zero-configuration: logi TRUE
## $ threads : int 1
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
To install from source on macOS you need either
imagemagick@6
or imagemagick
from
homebrew.
brew install imagemagick@6
Unfortunately the current imagemagick@6
configuration on
homebrew disables a bunch of features, including librsvg and fontconfig.
Therefore the quality of fonts and svg rendering might be suboptimal.
The is not a problem for the CRAN binary package.
What makes magick so magical is that it automatically converts and
renders all common image formats. ImageMagick supports dozens of formats
and automatically detects the type. Use
magick::magick_config()
to list the formats that your
version of ImageMagick supports.
Images can be read directly from a file path, URL, or raw vector with
image data with image_read
. The image_info
function shows some meta data about the image, similar to the
imagemagick identify
command line utility.
library(magick)
tiger <- image_read_svg('http://jeroen.github.io/images/tiger.svg', width = 350)
print(tiger)
## format width height colorspace matte filesize density
## 1 PNG 350 350 sRGB TRUE 0 72x72
We use image_write
to export an image in any format to a
file on disk, or in memory if path = NULL
.
# Render svg to png bitmap
image_write(tiger, path = "tiger.png", format = "png")
If path
is a filename, image_write
returns
path
on success such that the result can be piped into
function taking a file path.
Magick keeps the image in memory in its original format. Specify the
format
parameter image_write
to convert to
another format. You can also internally convert the image to another
format earlier, before applying transformations. This can be useful if
your original format is lossy.
tiger_png <- image_convert(tiger, "png")
image_info(tiger_png)
## format width height colorspace matte filesize density
## 1 PNG 350 350 sRGB TRUE 0 72x72
Note that size is currently 0 because ImageMagick is lazy (in the good sense) and does not render until it has to.
IDE’s with a built-in web browser (such as RStudio) automatically display magick images in the vi