You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
24
+
25
+
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.
23
26
24
27
```{r}
28
+
# Load the magick library
25
29
library(magick)
26
30
```
27
31
28
-
# input
32
+
# 1. Reading and Writing Images
33
+
34
+
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.
35
+
36
+
## Reading an Image
37
+
29
38
```{r}
39
+
# Read an image from a local file
30
40
raw_logo <- image_read('./images/comb.webp')
31
41
```
32
42
33
-
# output
43
+
## Getting Image Information
44
+
45
+
The `image_info()` function provides useful information about an image, such as its format, width, height, and colorspace.
34
46
35
47
```{r}
36
48
image_info(raw_logo)
37
49
```
38
50
39
-
#change format
51
+
## Changing Image Format
40
52
41
-
```{r}
42
-
new_png <- image_convert(raw_logo, "png")
43
-
```
53
+
The `image_convert()` function allows you to convert an image from one format to another.
44
54
45
55
```{r}
56
+
new_png <- image_convert(raw_logo, "png")
46
57
image_info(new_png)
47
58
```
48
59
49
-
# output
60
+
## Writing an Image
61
+
62
+
The `image_write()` function saves an image to a file. You can specify the path and format of the output file.
50
63
51
64
```{r}
52
65
image_write(new_png, path = "./images/new_png.png", format = "png")
53
66
```
54
67
68
+
# 2. Basic Image Manipulations
55
69
56
-
57
-
70
+
Let's read in another image to demonstrate some basic manipulations.
58
71
59
72
```{r}
60
73
raw_logo <- image_read('./images/logo1.png')
74
+
raw_logo
61
75
```
62
76
77
+
## Filling with Color
63
78
64
-
65
-
```{r}
66
-
raw_logo %>% print()
67
-
```
68
-
69
-
70
-
# fill corner white to greem
71
-
each corner (top left, top right, bottom left, bottom right). For our real usage, we’re going to convert this “green” space to transparent instead.
79
+
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_transparent()` function makes a specified color transparent. This is useful for creating images with transparent backgrounds.
108
110
109
111
```{r}
110
-
b=img_filled %>% image_colorize(opacity =80, color = 'white')
111
-
b %>% print()
112
+
# The fuzz argument (0-100) controls how similar colors need to be to be made transparent.
113
+
b = img_filled %>% image_transparent(color = 'green', fuzz = 10)
114
+
b
115
+
image_write(b, path = "images/b.png", format = "png")
112
116
```
113
117
114
-
# change brightness level
118
+
## Adjusting Opacity
119
+
120
+
The `image_colorize()` function can be used to adjust the opacity of an image.
115
121
116
122
```{r}
117
-
b=img_filled %>%image_modulate(brightness = 30)
118
-
119
-
b %>% print()
123
+
b = img_filled %>% image_colorize(opacity = 80, color = 'white')
124
+
b
120
125
```
121
126
122
-
# change blur level
127
+
## Adjusting Brightness
128
+
129
+
The `image_modulate()` function can be used to adjust the brightness, saturation, and hue of an image.
123
130
124
131
```{r}
125
-
b=img_filled %>% image_blur(10, 5)
126
-
127
-
b %>% print()
132
+
b = img_filled %>% image_modulate(brightness = 30)
133
+
b
128
134
```
129
135
136
+
## Applying a Blur Effect
130
137
131
-
# add text into picture
138
+
The `image_blur()` function applies a blur effect to an image. You can control the radius and sigma of the blur.
132
139
133
140
```{r}
134
-
b=img_filled %>% image_annotate( "The quick brown fox", font = 'Times', size = 80,gravity = "southwest", color = "red")
135
-
136
-
b %>% print()
141
+
b = img_filled %>% image_blur(10, 5)
142
+
b
137
143
```
138
-
# resize
139
144
140
-
using image_resize or image_scale
141
-
```{r}
142
-
b= img_filled%>% image_resize("500")
143
-
b %>% print()
144
-
```
145
+
## Adding Text to an Image
145
146
147
+
The `image_annotate()` function adds text to an image. You can specify the text, font, size, color, and gravity (position).
146
148
147
149
```{r}
148
-
image_info(img_filled)
150
+
b = img_filled %>% image_annotate("The quick brown fox", font = 'Times', size = 20, gravity = "southwest", color = "red")
151
+
b
149
152
```
150
153
154
+
## Resizing an Image
155
+
156
+
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.
157
+
151
158
```{r}
159
+
b = img_filled %>% image_resize("200x200")
160
+
b
152
161
image_info(b)
153
162
```
154
-
# make logo black
163
+
164
+
# 3. Advanced Image Manipulations
165
+
166
+
## Creating a Black and White Logo
167
+
168
+
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.
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.
248
258
249
-
# resource:
250
-
259
+
For more information and examples, please refer to the following resources:
0 commit comments