Code
library(magick)
with magick
Tony Duan
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.
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 %>% print()
format width height colorspace matte filesize density
1 PNG 1344 960 sRGB TRUE 0 72x72
using image_resize or image_scale ::: {.cell}
format width height colorspace matte filesize density
1 PNG 500 357 sRGB TRUE 0 72x72
:::
format width height colorspace matte filesize density
1 PNG 1344 960 sRGB TRUE 0 72x72
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")
format width height colorspace matte filesize density
1 PNG 1344 960 Gray FALSE 0 72x72
R version 4.4.1 (2024-06-14)
Platform: aarch64-apple-darwin20
Running under: macOS 15.1
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.0
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
time zone: Asia/Shanghai
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] magick_2.8.5
loaded via a namespace (and not attached):
[1] digest_0.6.37 fastmap_1.2.0 xfun_0.48 magrittr_2.0.3
[5] knitr_1.48 htmltools_0.5.8.1 png_0.1-8 rmarkdown_2.28
[9] cli_3.6.4 compiler_4.4.1 rstudioapi_0.17.1 tools_4.4.1
[13] evaluate_1.0.1 Rcpp_1.0.13-1 yaml_2.3.10 rlang_1.1.5
[17] jsonlite_1.8.9 htmlwidgets_1.6.4
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)
library(cowplot)
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)
https://themockup.blog/posts/2021-01-28-removing-image-backgrounds-with-magick/
https://cran.r-project.org/web/packages/magick/vignettes/intro.html
https://buzzrbeeline.blog/2018/06/13/fun-and-easy-r-graphs-with-images/
---
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: true
code-block-bg: true
code-block-border-left: "#31BAE9"
code-copy: true
---
```{r}
library(magick)
```
# input
```{r}
raw_logo <- image_read('./images/comb.webp')
```
# output
```{r}
image_info(raw_logo)
```
# change format
```{r}
new_png <- image_convert(raw_logo, "png")
```
```{r}
image_info(new_png)
```
# output
```{r}
image_write(new_png, path = "./images/new_png.png", format = "png")
```
```{r}
raw_logo <- image_read('./images/logo1.png')
```
```{r}
raw_logo %>% print()
```
# fill corner white to greem
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.
```{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 %>% print()
```
# rotate
```{r}
img_filled %>% image_rotate(45) %>% print()
```
# add border
```{r}
img_filled %>% image_border("#CD5C5C","20x20")
```
# make backgroup transparent
```{r}
# 0-100 distance fuzz match with green
b=img_filled %>% image_transparent(color='green',10)
b %>% print()
image_write(b, path = "b.png", format = "png")
```
# change opacity level
```{r}
b=img_filled %>% image_colorize(opacity =80, color = 'white')
b %>% print()
```
# change brightness level
```{r}
b=img_filled %>%image_modulate(brightness = 30)
b %>% print()
```
# change blur level
```{r}
b=img_filled %>% image_blur(10, 5)
b %>% print()
```
# add text into picture
```{r}
b=img_filled %>% image_annotate( "The quick brown fox", font = 'Times', size = 80,gravity = "southwest", color = "red")
b %>% print()
```
# resize
using image_resize or image_scale
```{r}
b= img_filled%>% image_resize("500")
b %>% print()
```
```{r}
image_info(img_filled)
```
```{r}
image_info(b)
```
# make logo black
```{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")
```
```{r}
img_filled2 %>%
image_channel("Opacity") %>%
image_convert(matte=FALSE) %>%
print()
```
```{r, attr.output='.details summary="sessionInfo()"'}
#| echo: false
sessionInfo()
```
# add backgroup image
```{r}
library(ggplot2)
library(png)
library(grid)
library(ggimage)
```
```{r}
img <- readPNG("./images/new_png.png")
```
```{r}
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)
```
# replace scatter with image
```{r}
bees$image <- "./images/bee.png"
```
```{r}
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)
```
# add logo
```{r}
img2 =image_read("./images/logo1.png")
```
```{r}
library(cowplot)
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)
```
# resource:
https://themockup.blog/posts/2021-01-28-removing-image-backgrounds-with-magick/
https://cran.r-project.org/web/packages/magick/vignettes/intro.html
https://buzzrbeeline.blog/2018/06/13/fun-and-easy-r-graphs-with-images/