I wonder if there is an automated way to align several microscopic images together in R to appear in such form like this image (source: doi: https://doi.org/10.1101/2019.12.11.873471). I am not looking for image analysis, only grid the images and add labels. I tried with
magick
in which I was able to write labels on the images and with gridExtra
to align them and add the labels outside. but still not satisfied because I wasn't able to control the distance between the blocks and add labels in boxes like the image above.
can anybody recommend a package to do something like this ( maybe in R or Python, not sure if Latex can do something like that also?).
below is my reproducible code and what I get.
Many thanks for your help.
library(magick)
library(grid)
library(gridExtra)
names_of_images <- LETTERS[1:16]
pic_url <- "https://imagehost.imageupload.net/2020/04/30/EXAMPLE.jpg"
pic_cat <- tempfile()
download.file(pic_url,pic_cat,mode="wb")
pic <- image_read(pic_cat)
## write labels on images
image_listall <- list()
for ( i in names_of_images ) {
im_read_bor <- image_border(pic , 'white' ,geometry = "10x10")
im_read_bor_anno <- image_annotate(im_read_bor, paste(i),
size = 100, color = "white" , location = "+50+40" )
image_listall[[i]] = im_read_bor_anno
}
## arrange images in rows
row1 <- image_append(c(image_listall$A, image_listall$B ,image_listall$C,image_listall$D ), stack = F)
row2 <- image_append(c(image_listall$E, image_listall$F ,image_listall$G,image_listall$H ), stack = F)
row3 <- image_append(c(image_listall$I, image_listall$J,image_listall$K,image_listall$L) ,stack = F)
row4 <- image_append(c(image_listall$M, image_listall$N,image_listall$O, image_listall$P) ,stack = F)
## now add row labels and title
r1 <- grid.arrange(rasterGrob(row1) , top = textGrob(
"First Block",just = "center",
gp = gpar(fontface = 'bold', fontsize = 18)) ,
left = textGrob(
"(A)",
gp = gpar(fontface = 'bold', fontsize = 15)))
r2 <- grid.arrange(rasterGrob(row2) ,
left = textGrob(
"(B)",
gp = gpar(fontface = 'bold', fontsize = 15)))
r3 <- grid.arrange(rasterGrob(row3) , top = textGrob(
"Second Block", just = "center",
gp = gpar(fontface = 'bold', fontsize = 18)) ,
left = textGrob(
"(C)",
gp = gpar(fontface = 'bold', fontsize = 15)))
r4 <- grid.arrange(rasterGrob(row4) ,
left = textGrob(
"(D)",
gp = gpar(fontface = 'bold', fontsize = 15)))
## draw all together
grid.draw(rbind(r1, r2,r3,r4, size = "last"))
it looks like this: