Skip to content

Commit 7c6edbc

Browse files
committed
w
1 parent 6641469 commit 7c6edbc

3,752 files changed

Lines changed: 604633 additions & 20278 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.DS_Store

2 KB
Binary file not shown.

Plot/.DS_Store

2 KB
Binary file not shown.

Plot/1 ggplot2.qmd

Lines changed: 306 additions & 257 deletions
Large diffs are not rendered by default.

Plot/2 plotly.qmd

Lines changed: 247 additions & 161 deletions
Large diffs are not rendered by default.

Plot/3 image processing.qmd

Lines changed: 105 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -14,61 +14,69 @@ format:
1414
toc-location: right
1515
code-fold: show
1616
code-tools: true
17-
number-sections: true
17+
number-sections: false
1818
code-block-bg: true
1919
code-block-border-left: "#31BAE9"
2020
code-copy: true
2121
---
2222

23+
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.
2326

2427
```{r}
28+
# Load the magick library
2529
library(magick)
2630
```
2731

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+
2938
```{r}
39+
# Read an image from a local file
3040
raw_logo <- image_read('./images/comb.webp')
3141
```
3242

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.
3446

3547
```{r}
3648
image_info(raw_logo)
3749
```
3850

39-
# change format
51+
## Changing Image Format
4052

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.
4454

4555
```{r}
56+
new_png <- image_convert(raw_logo, "png")
4657
image_info(new_png)
4758
```
4859

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.
5063

5164
```{r}
5265
image_write(new_png, path = "./images/new_png.png", format = "png")
5366
```
5467

68+
# 2. Basic Image Manipulations
5569

56-
57-
70+
Let's read in another image to demonstrate some basic manipulations.
5871

5972
```{r}
6073
raw_logo <- image_read('./images/logo1.png')
74+
raw_logo
6175
```
6276

77+
## Filling with Color
6378

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.
7280

7381
```{r}
7482
img_filled <- raw_logo %>%
@@ -77,181 +85,182 @@ img_filled <- raw_logo %>%
7785
image_fill("green", "+1+99", fuzz = 50, refcolor = "white") %>%
7886
image_fill("green", "+140+99", fuzz = 50, refcolor = "white")
7987
80-
img_filled %>% print()
88+
img_filled
8189
```
82-
# rotate
8390

84-
```{r}
85-
img_filled %>% image_rotate(45) %>% print()
86-
```
91+
## Rotating an Image
8792

88-
# add border
93+
The `image_rotate()` function rotates an image by a specified angle.
8994

9095
```{r}
91-
img_filled %>% image_border("#CD5C5C","20x20")
96+
img_filled %>% image_rotate(45)
9297
```
9398

99+
## Adding a Border
94100

95-
96-
# make backgroup transparent
101+
The `image_border()` function adds a border to an image. You can specify the color and size of the border.
97102

98103
```{r}
99-
# 0-100 distance fuzz match with green
100-
b=img_filled %>% image_transparent(color='green',10)
101-
102-
b %>% print()
103-
104-
image_write(b, path = "b.png", format = "png")
104+
img_filled %>% image_border("#CD5C5C", "20x20")
105105
```
106106

107-
# change opacity level
107+
## Making a Background Transparent
108+
109+
The `image_transparent()` function makes a specified color transparent. This is useful for creating images with transparent backgrounds.
108110

109111
```{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")
112116
```
113117

114-
# change brightness level
118+
## Adjusting Opacity
119+
120+
The `image_colorize()` function can be used to adjust the opacity of an image.
115121

116122
```{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
120125
```
121126

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.
123130

124131
```{r}
125-
b=img_filled %>% image_blur(10, 5)
126-
127-
b %>% print()
132+
b = img_filled %>% image_modulate(brightness = 30)
133+
b
128134
```
129135

136+
## Applying a Blur Effect
130137

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.
132139

133140
```{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
137143
```
138-
# resize
139144

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
145146

147+
The `image_annotate()` function adds text to an image. You can specify the text, font, size, color, and gravity (position).
146148

147149
```{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
149152
```
150153

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+
151158
```{r}
159+
b = img_filled %>% image_resize("200x200")
160+
b
152161
image_info(b)
153162
```
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.
169+
155170
```{r}
156171
img_filled2 <- raw_logo %>%
157172
image_fill("transparent", "+1+1", fuzz = 50, refcolor = "white") %>%
158173
image_fill("transparent", "+140+1", fuzz = 50, refcolor = "white") %>%
159174
image_fill("transparent", "+1+99", fuzz = 50, refcolor = "white") %>%
160175
image_fill("transparent", "+140+99", fuzz = 50, refcolor = "white")
161-
```
162176
163-
```{r}
164177
img_filled2 %>%
165178
image_channel("Opacity") %>%
166-
image_convert(matte=FALSE) %>%
167-
print()
179+
image_convert(matte = FALSE)
168180
```
169181

170-
```{r, attr.output='.details summary="sessionInfo()"'}
171-
#| echo: false
172-
sessionInfo()
173-
```
182+
# 4. Integrating `magick` with `ggplot2`
174183

184+
One of the most powerful features of `magick` is its ability to be integrated with `ggplot2` to create more visually interesting plots.
175185

186+
## Adding a Background Image to a Plot
176187

177-
# add backgroup image
188+
We can use `annotation_custom()` and `rasterGrob()` to add an image as the background of a `ggplot2` plot.
178189

179190
```{r}
180191
library(ggplot2)
181192
library(png)
182193
library(grid)
183-
library(ggimage)
184-
```
185-
186194
187-
```{r}
188195
img <- readPNG("./images/new_png.png")
189-
```
190-
191-
```{r}
192196
193197
bees <- data.frame(distance = c(0.5, 1, 1.5, 2, 2.5, 3),
194-
number = c(40, 34, 32, 22,18, 10))
198+
number = c(40, 34, 32, 22, 18, 10))
195199
196200
ggplot(data = bees, aes(x = distance, y = number)) +
197201
annotation_custom(rasterGrob(img,
198-
width = unit(1,"npc"),
199-
height = unit(1,"npc")),
202+
width = unit(1, "npc"),
203+
height = unit(1, "npc")),
200204
-Inf, Inf, -Inf, Inf) +
201205
geom_point() +
202206
xlab("Distance (km)") +
203207
ylab("Number of Bees") +
204208
ylim(0, 45)
205209
```
206-
# replace scatter with image
207210

208-
```{r}
209-
bees$image <- "./images/bee.png"
210-
```
211+
## Replacing Scatter Points with Images
211212

213+
The `ggimage` package provides `geom_image()`, which allows you to use images as points in a scatter plot.
212214

213215
```{r}
216+
library(ggimage)
217+
218+
bees$image <- "./images/bee.png"
219+
214220
ggplot(data = bees, aes(x = distance, y = number)) +
215221
annotation_custom(rasterGrob(img,
216-
width = unit(1,"npc"),
217-
height = unit(1,"npc")),
222+
width = unit(1, "npc"),
223+
height = unit(1, "npc")),
218224
-Inf, Inf, -Inf, Inf) +
219225
geom_image(aes(image = image), size = 0.15) +
220226
xlab("Distance (km)") +
221227
ylab("Number of Bees") +
222228
ylim(0, 45)
223229
```
224-
# add logo
225230

226-
```{r}
227-
img2 =image_read("./images/logo1.png")
228-
```
231+
## Adding a Logo to a Plot
232+
233+
The `cowplot` package provides `ggdraw()` and `draw_image()` which make it easy to add a logo or other images to your `ggplot2` plots.
229234

230235
```{r}
231236
library(cowplot)
232-
p=ggplot(data = bees, aes(x = distance, y = number)) +
237+
238+
img2 = image_read("./images/logo1.png")
239+
240+
p = ggplot(data = bees, aes(x = distance, y = number)) +
233241
annotation_custom(rasterGrob(img,
234-
width = unit(1,"npc"),
235-
height = unit(1,"npc")),
242+
width = unit(1, "npc"),
243+
height = unit(1, "npc")),
236244
-Inf, Inf, -Inf, Inf) +
237245
geom_image(aes(image = image), size = 0.15) +
238246
xlab("Distance (km)") +
239247
ylab("Number of Bees") +
240248
ylim(0, 45)
241249
242-
243-
244-
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)
250+
ggdraw() +
251+
draw_plot(p, x = 0, y = 0.15, width = 1, height = 0.85) +
252+
draw_image(img2, x = 0.1, y = 0.1, width = 0.1, height = 0.1)
245253
```
246254

255+
# 5. Conclusion and Further Resources
247256

257+
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.
248258

249-
# resource:
250-
259+
For more information and examples, please refer to the following resources:
251260

252-
https://themockup.blog/posts/2021-01-28-removing-image-backgrounds-with-magick/
261+
- [The `magick` Package Vignette](httpshttps://cran.r-project.org/web/packages/magick/vignettes/intro.html)
262+
- [Removing Image Backgrounds with `magick`](https://themockup.blog/posts/2021-01-28-removing-image-backgrounds-with-magick/)
263+
- [Fun and Easy R Graphs with Images](https://buzzrbeeline.blog/2018/06/13/fun-and-easy-r-graphs-with-images/)
253264

254-
https://cran.r-project.org/web/packages/magick/vignettes/intro.html
255265

256-
https://buzzrbeeline.blog/2018/06/13/fun-and-easy-r-graphs-with-images/
257266

0 commit comments

Comments
 (0)