Search code examples
rmagick

Add white rectangle to bottom of image with imageMagick in R


I wish to add a white rectangle to the bottom of a .png image using R's magick library.

Dimensions of White Rectangle

Width: 1200px

Height: 50px

I think append would be useful. However, it's not clear to me how to create as opposed to read, a white rectangular image. To be specific, how do I add a white rectangle to the bottom of this car image that is 50px in height and runs the length of the image.

library("magick")
url <- "https://images.unsplash.com/photo-1604397707219-dfe825b8a59d"
 

Solution

  • Use image_blank to create the white rectangle. Then combine the images using image_composite.

    library(magick)
    url <- "https://images.unsplash.com/photo-1604397707219-dfe825b8a59d"
    
    img <- image_read(url)
    white <- image_blank(1200, 50, "white")
    
    x <- image_info(img)$width - 1200
    y <- image_info(img)$height - 50
    
    image_composite(img, white, offset = paste0("+", x, "+", y))