ccamara
November 16, 2021, 9:50am
1
Hello, I have thousands of images that I’d like to combine in sets of four images using a specific layout (top-centre, right-middle, bottom-centre, left-middle), as displayed in this image:
As far as I know, I could use magick’s image_montage()
to combine them into a single file, but I have not found any way to control their position other than horizontal row (default) or vertical column.
Is there any way to achieve what I am looking for?
2 Likes
You can use image_composite which has an offset and gravity parameter to control positioning. For example:
library(magick)
bowser <- image_read('https://upload.wikimedia.org/wikipedia/en/9/92/Bowser_Stock_Art_2021.png') %>% image_scale('200x')
yoshi <- image_read('https://upload.wikimedia.org/wikipedia/en/d/db/Yoshi_%28Nintendo_character%29.png') %>% image_scale('200x')
peach <- image_read('https://upload.wikimedia.org/wikipedia/en/1/16/Princess_Peach_Stock_Art.png') %>% image_scale('200x')
toad <- image_read('https://upload.wikimedia.org/wikipedia/en/d/d1/Toad_3D_Land.png') %>% image_scale('200x')
image_blank(1000, 1000, color = 'white') %>%
image_composite(bowser, offset = '+100+100') %>%
image_composite(yoshi, offset = '+100+100', gravity = 'northeast') %>%
image_composite(peach, offset = '+100+100', gravity = 'southwest') %>%
image_composite(toad, offset = '+100+100', gravity = 'southeast')
3 Likes
ccamara
November 16, 2021, 10:44am
3
Thank you for your quick response, @jeroenooms . It is indeed what I was looking for!
I could do what I was looking for with this code (adapted from yours):
library(magick)
bowser <- image_read('https://upload.wikimedia.org/wikipedia/en/9/92/Bowser_Stock_Art_2021.png') %>% image_scale('200x')
yoshi <- image_read('https://upload.wikimedia.org/wikipedia/en/d/db/Yoshi_%28Nintendo_character%29.png') %>% image_scale('200x')
peach <- image_read('https://upload.wikimedia.org/wikipedia/en/1/16/Princess_Peach_Stock_Art.png') %>% image_scale('200x')
toad <- image_read('https://upload.wikimedia.org/wikipedia/en/d/d1/Toad_3D_Land.png') %>% image_scale('200x')
image_blank(1000, 1000, color = 'white') %>%
image_composite(bowser, gravity = 'north') %>%
image_composite(yoshi, gravity = 'east') %>%
image_composite(peach, gravity = 'south') %>%
image_composite(toad, gravity = 'west')
Now I’ll be investigating how to save that image into a file.
Thanks fro your help!
EDIT: here it is! The magick package: Advanced Image-Processing in R • rOpenSci: magick
1 Like