with magick

Author

Tony Duan

Code
library(magick)

1 input

Code
raw_logo <- image_read('./images/comb.webp')

2 output

Code
image_info(raw_logo)
  format width height colorspace matte filesize density
1   WEBP   450    303       sRGB  TRUE    49650   72x72

3 change format

Code
new_png <- image_convert(raw_logo, "png")
Code
image_info(new_png)
  format width height colorspace matte filesize density
1    PNG   450    303       sRGB  TRUE        0   72x72

4 output

Code
image_write(new_png, path = "./images/new_png.png", format = "png")
Code
raw_logo <- image_read('./images/logo1.png')
Code
raw_logo %>% print()
  format width height colorspace matte filesize density
1    PNG  1344    960       sRGB  TRUE    53960   72x72

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

Code
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

6 rotate

Code
img_filled %>% image_rotate(45) %>% print()
  format width height colorspace matte filesize density
1    PNG  1632   1632       sRGB  TRUE        0   72x72

7 add border

Code
img_filled %>% image_border("#CD5C5C","20x20")

8 make backgroup transparent

Code
# 0-100 distance fuzz match with green
b=img_filled %>% image_transparent(color='green',10)

b %>% print()
  format width height colorspace matte filesize density
1    PNG  1344    960       sRGB  TRUE        0   72x72

Code
image_write(b, path = "b.png", format = "png")

9 change opacity level

Code
b=img_filled %>% image_colorize(opacity =80, color = 'white')
b %>% print()
  format width height colorspace matte filesize density
1    PNG  1344    960       sRGB  TRUE        0   72x72

10 change brightness level

Code
b=img_filled %>%image_modulate(brightness = 30)
  
b %>% print()
  format width height colorspace matte filesize density
1    PNG  1344    960       sRGB  TRUE        0   72x72

11 change blur level

Code
b=img_filled %>%  image_blur(10, 5)
  
b %>% print()
  format width height colorspace matte filesize density
1    PNG  1344    960       sRGB  TRUE        0   72x72

12 add text into picture

Code
b=img_filled %>% image_annotate( "The quick brown fox", font = 'Times', size = 80,gravity = "southwest", color = "red")
  
b %>% print()
  format width height colorspace matte filesize density
1    PNG  1344    960       sRGB  TRUE        0   72x72

13 resize

using image_resize or image_scale ::: {.cell}

Code
b= img_filled%>% image_resize("500")
b %>% print()
  format width height colorspace matte filesize density
1    PNG   500    357       sRGB  TRUE        0   72x72

:::

Code
image_info(img_filled)
  format width height colorspace matte filesize density
1    PNG  1344    960       sRGB  TRUE        0   72x72
Code
image_info(b)
  format width height colorspace matte filesize density
1    PNG   500    357       sRGB  TRUE        0   72x72

14 make logo black

Code
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")
Code
img_filled2 %>% 
    image_channel("Opacity") %>% 
    image_convert(matte=FALSE) %>% 
  print()
  format width height colorspace matte filesize density
1    PNG  1344    960       Gray FALSE        0   72x72

sessionInfo()
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

15 add backgroup image

Code
library(ggplot2)
library(png)
library(grid)
library(ggimage)
Code
img <- readPNG("./images/new_png.png")
Code
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)

16 replace scatter with image

Code
bees$image <- "./images/bee.png"
Code
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)

18 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/

Back to top