Code
# Load the magick library
library(magick)
with magick
Tony Duan
This document provides a practical guide to image processing in R using the magick
package. The magick
package is a powerful and versatile tool for image manipulation, providing a wide range of functions for reading, writing, editing, and transforming images. It is built on top of the ImageMagick library, a robust and feature-rich image processing software.
We will cover the basics of reading and writing images, as well as more advanced topics like color manipulation, transformations, and adding text and borders. We will also explore how to integrate magick
with ggplot2
to create more visually appealing and informative plots.
The first step in any image processing workflow is to read an image into R. The image_read()
function can read images from a local file or a URL.
The image_info()
function provides useful information about an image, such as its format, width, height, and colorspace.
The image_convert()
function allows you to convert an image from one format to another.
The image_write()
function saves an image to a file. You can specify the path and format of the output file.
Let’s read in another image to demonstrate some basic manipulations.
The image_fill()
function can be used to fill areas of an image with a specified color. Here, we fill the white corners of the logo with green. The fuzz
argument controls the tolerance of the color matching.
The image_rotate()
function rotates an image by a specified angle.
The image_border()
function adds a border to an image. You can specify the color and size of the border.
The image_transparent()
function makes a specified color transparent. This is useful for creating images with transparent backgrounds.
The image_colorize()
function can be used to adjust the opacity of an image.
The image_modulate()
function can be used to adjust the brightness, saturation, and hue of an image.
The image_blur()
function applies a blur effect to an image. You can control the radius and sigma of the blur.
The image_annotate()
function adds text to an image. You can specify the text, font, size, color, and gravity (position).
The image_resize()
and image_scale()
functions can be used to resize an image. image_resize()
allows you to specify the new dimensions, while image_scale()
scales the image by a factor.
By combining several magick
functions, we can perform more complex image manipulations. Here, we create a black and white version of the logo with a transparent background.
img_filled2 <- raw_logo %>%
image_fill("transparent", "+1+1", fuzz = 50, refcolor = "white") %>%
image_fill("transparent", "+140+1", fuzz = 50, refcolor = "white") %>%
image_fill("transparent", "+1+99", fuzz = 50, refcolor = "white") %>%
image_fill("transparent", "+140+99", fuzz = 50, refcolor = "white")
img_filled2 %>%
image_channel("Opacity") %>%
image_convert(matte = FALSE)
magick
with ggplot2
One of the most powerful features of magick
is its ability to be integrated with ggplot2
to create more visually interesting plots.
We can use annotation_custom()
and rasterGrob()
to add an image as the background of a ggplot2
plot.
library(ggplot2)
library(png)
library(grid)
img <- readPNG("./images/new_png.png")
bees <- data.frame(distance = c(0.5, 1, 1.5, 2, 2.5, 3),
number = c(40, 34, 32, 22, 18, 10))
ggplot(data = bees, aes(x = distance, y = number)) +
annotation_custom(rasterGrob(img,
width = unit(1, "npc"),
height = unit(1, "npc")),
-Inf, Inf, -Inf, Inf) +
geom_point() +
xlab("Distance (km)") +
ylab("Number of Bees") +
ylim(0, 45)
The ggimage
package provides geom_image()
, which allows you to use images as points in a scatter plot.
library(ggimage)
bees$image <- "./images/bee.png"
ggplot(data = bees, aes(x = distance, y = number)) +
annotation_custom(rasterGrob(img,
width = unit(1, "npc"),
height = unit(1, "npc")),
-Inf, Inf, -Inf, Inf) +
geom_image(aes(image = image), size = 0.15) +
xlab("Distance (km)") +
ylab("Number of Bees") +
ylim(0, 45)
The cowplot
package provides ggdraw()
and draw_image()
which make it easy to add a logo or other images to your ggplot2
plots.
library(cowplot)
img2 = image_read("./images/logo1.png")
p = ggplot(data = bees, aes(x = distance, y = number)) +
annotation_custom(rasterGrob(img,
width = unit(1, "npc"),
height = unit(1, "npc")),
-Inf, Inf, -Inf, Inf) +
geom_image(aes(image = image), size = 0.15) +
xlab("Distance (km)") +
ylab("Number of Bees") +
ylim(0, 45)
ggdraw() +
draw_plot(p, x = 0, y = 0.15, width = 1, height = 0.85) +
draw_image(img2, x = 0.1, y = 0.1, width = 0.1, height = 0.1)
This document has provided a comprehensive introduction to the magick
package in R. You have learned how to read, write, and manipulate images, as well as how to integrate them with ggplot2
to create more engaging visualizations.
For more information and examples, please refer to the following resources:
---
title: "image processing"
subtitle: "with magick"
author: "Tony Duan"
execute:
warning: false
error: false
format:
html:
toc: true
toc-location: right
code-fold: show
code-tools: true
number-sections: false
code-block-bg: true
code-block-border-left: "#31BAE9"
code-copy: true
---
This document provides a practical guide to image processing in R using the `magick` package. The `magick` package is a powerful and versatile tool for image manipulation, providing a wide range of functions for reading, writing, editing, and transforming images. It is built on top of the ImageMagick library, a robust and feature-rich image processing software.
We will cover the basics of reading and writing images, as well as more advanced topics like color manipulation, transformations, and adding text and borders. We will also explore how to integrate `magick` with `ggplot2` to create more visually appealing and informative plots.
```{r}
# Load the magick library
library(magick)
```
# 1. Reading and Writing Images
The first step in any image processing workflow is to read an image into R. The `image_read()` function can read images from a local file or a URL.
## Reading an Image
```{r}
# Read an image from a local file
raw_logo <- image_read('./images/comb.webp')
```
## Getting Image Information
The `image_info()` function provides useful information about an image, such as its format, width, height, and colorspace.
```{r}
image_info(raw_logo)
```
## Changing Image Format
The `image_convert()` function allows you to convert an image from one format to another.
```{r}
new_png <- image_convert(raw_logo, "png")
image_info(new_png)
```
## Writing an Image
The `image_write()` function saves an image to a file. You can specify the path and format of the output file.
```{r}
image_write(new_png, path = "./images/new_png.png", format = "png")
```
# 2. Basic Image Manipulations
Let's read in another image to demonstrate some basic manipulations.
```{r}
raw_logo <- image_read('./images/logo1.png')
raw_logo
```
## Filling with Color
The `image_fill()` function can be used to fill areas of an image with a specified color. Here, we fill the white corners of the logo with green. The `fuzz` argument controls the tolerance of the color matching.
```{r}
img_filled <- raw_logo %>%
image_fill("green", "+1+1", fuzz = 50, refcolor = "white") %>%
image_fill("green", "+140+1", fuzz = 50, refcolor = "white") %>%
image_fill("green", "+1+99", fuzz = 50, refcolor = "white") %>%
image_fill("green", "+140+99", fuzz = 50, refcolor = "white")
img_filled
```
## Rotating an Image
The `image_rotate()` function rotates an image by a specified angle.
```{r}
img_filled %>% image_rotate(45)
```
## Adding a Border
The `image_border()` function adds a border to an image. You can specify the color and size of the border.
```{r}
img_filled %>% image_border("#CD5C5C", "20x20")
```
## Making a Background Transparent
The `image_transparent()` function makes a specified color transparent. This is useful for creating images with transparent backgrounds.
```{r}
# The fuzz argument (0-100) controls how similar colors need to be to be made transparent.
b = img_filled %>% image_transparent(color = 'green', fuzz = 10)
b
image_write(b, path = "images/b.png", format = "png")
```
## Adjusting Opacity
The `image_colorize()` function can be used to adjust the opacity of an image.
```{r}
b = img_filled %>% image_colorize(opacity = 80, color = 'white')
b
```
## Adjusting Brightness
The `image_modulate()` function can be used to adjust the brightness, saturation, and hue of an image.
```{r}
b = img_filled %>% image_modulate(brightness = 30)
b
```
## Applying a Blur Effect
The `image_blur()` function applies a blur effect to an image. You can control the radius and sigma of the blur.
```{r}
b = img_filled %>% image_blur(10, 5)
b
```
## Adding Text to an Image
The `image_annotate()` function adds text to an image. You can specify the text, font, size, color, and gravity (position).
```{r}
b = img_filled %>% image_annotate("The quick brown fox", font = 'Times', size = 20, gravity = "southwest", color = "red")
b
```
## Resizing an Image
The `image_resize()` and `image_scale()` functions can be used to resize an image. `image_resize()` allows you to specify the new dimensions, while `image_scale()` scales the image by a factor.
```{r}
b = img_filled %>% image_resize("200x200")
b
image_info(b)
```
# 3. Advanced Image Manipulations
## Creating a Black and White Logo
By combining several `magick` functions, we can perform more complex image manipulations. Here, we create a black and white version of the logo with a transparent background.
```{r}
img_filled2 <- raw_logo %>%
image_fill("transparent", "+1+1", fuzz = 50, refcolor = "white") %>%
image_fill("transparent", "+140+1", fuzz = 50, refcolor = "white") %>%
image_fill("transparent", "+1+99", fuzz = 50, refcolor = "white") %>%
image_fill("transparent", "+140+99", fuzz = 50, refcolor = "white")
img_filled2 %>%
image_channel("Opacity") %>%
image_convert(matte = FALSE)
```
# 4. Integrating `magick` with `ggplot2`
One of the most powerful features of `magick` is its ability to be integrated with `ggplot2` to create more visually interesting plots.
## Adding a Background Image to a Plot
We can use `annotation_custom()` and `rasterGrob()` to add an image as the background of a `ggplot2` plot.
```{r}
library(ggplot2)
library(png)
library(grid)
img <- readPNG("./images/new_png.png")
bees <- data.frame(distance = c(0.5, 1, 1.5, 2, 2.5, 3),
number = c(40, 34, 32, 22, 18, 10))
ggplot(data = bees, aes(x = distance, y = number)) +
annotation_custom(rasterGrob(img,
width = unit(1, "npc"),
height = unit(1, "npc")),
-Inf, Inf, -Inf, Inf) +
geom_point() +
xlab("Distance (km)") +
ylab("Number of Bees") +
ylim(0, 45)
```
## Replacing Scatter Points with Images
The `ggimage` package provides `geom_image()`, which allows you to use images as points in a scatter plot.
```{r}
library(ggimage)
bees$image <- "./images/bee.png"
ggplot(data = bees, aes(x = distance, y = number)) +
annotation_custom(rasterGrob(img,
width = unit(1, "npc"),
height = unit(1, "npc")),
-Inf, Inf, -Inf, Inf) +
geom_image(aes(image = image), size = 0.15) +
xlab("Distance (km)") +
ylab("Number of Bees") +
ylim(0, 45)
```
## Adding a Logo to a Plot
The `cowplot` package provides `ggdraw()` and `draw_image()` which make it easy to add a logo or other images to your `ggplot2` plots.
```{r}
library(cowplot)
img2 = image_read("./images/logo1.png")
p = ggplot(data = bees, aes(x = distance, y = number)) +
annotation_custom(rasterGrob(img,
width = unit(1, "npc"),
height = unit(1, "npc")),
-Inf, Inf, -Inf, Inf) +
geom_image(aes(image = image), size = 0.15) +
xlab("Distance (km)") +
ylab("Number of Bees") +
ylim(0, 45)
ggdraw() +
draw_plot(p, x = 0, y = 0.15, width = 1, height = 0.85) +
draw_image(img2, x = 0.1, y = 0.1, width = 0.1, height = 0.1)
```
# 5. Conclusion and Further Resources
This document has provided a comprehensive introduction to the `magick` package in R. You have learned how to read, write, and manipulate images, as well as how to integrate them with `ggplot2` to create more engaging visualizations.
For more information and examples, please refer to the following resources:
- [The `magick` Package Vignette](httpshttps://cran.r-project.org/web/packages/magick/vignettes/intro.html)
- [Removing Image Backgrounds with `magick`](https://themockup.blog/posts/2021-01-28-removing-image-backgrounds-with-magick/)
- [Fun and Easy R Graphs with Images](https://buzzrbeeline.blog/2018/06/13/fun-and-easy-r-graphs-with-images/)