Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save jthomasmock/e1e7bfa83fd39155f6b042726ffb3e9b to your computer and use it in GitHub Desktop.

Select an option

Save jthomasmock/e1e7bfa83fd39155f6b042726ffb3e9b to your computer and use it in GitHub Desktop.
library(magick)
create_overlay <- function(img, overlay_color = "#00000060", out_file = NULL){
if(!("magick-image" %in% class(img))){
raw_img <- image_read(img)
} else if ("magick-image" %in% class(img)){
raw_img <- img
}
# get image dimensions
img_info <- image_info(raw_img)
# draw the raw image
img_overlay <- image_draw(raw_img)
# draw a rectangle of equal proportion to the raw image
rect(0, 0, img_info$width, img_info$height, col = overlay_color, border = NA)
# save the results
dev.off()
# return it or return + save out
if(!is.null(out_file)){
image_write(img_overlay, path = out_file)
message(paste("Image saved as", out_file))
return(img_overlay)
} else {
return(img_overlay)
}
}
headshot <- "https://mayfieldsportsmarketing.com/wp-content/uploads/2019/08/Untitled-design-2019-08-22T141138.123.png"
create_overlay(headshot, out_file = "aj-shaded.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment