Skip to content

Instantly share code, notes, and snippets.

@mplatzer
Created June 29, 2020 17:00
Show Gist options
  • Save mplatzer/58462e3582a84fc776692819719ec3f3 to your computer and use it in GitHub Desktop.
Save mplatzer/58462e3582a84fc776692819719ec3f3 to your computer and use it in GitHub Desktop.
resize & crop photos to 10:15, plus add a white border
library(magick)
fns <- list.files(patt='jpg')
lapply(fns, function(fn) {
cat(basename(fn), '\n')
img <- image_read(fn)
cdate <- exif_read(fn)$CreateDate
if (!is.null(cdate)) {
cdate <- as.Date(cdate, format='%Y:%m:%d')
} else {
cdate <- Sys.Date()
}
img <- image_page(img, density = '300x300')
w <- image_info(img)$width
h <- image_info(img)$height
if (w < h) {
img <- image_rotate(img, 90)
w <- image_info(img)$width
h <- image_info(img)$height
}
stopifnot(w > h)
if (round(w/h, 2) > 1.5) {
img <- image_crop(img, geometry = geometry_area(width = h*1.5, height = h), repage=FALSE)
} else {
img <- image_crop(img, geometry = geometry_area(width = w, height = w/1.5), repage=FALSE)
}
img <- image_resize(img, geometry = geometry_area(width = 1500, height = 1000))
img <- image_border(img, color = 'white', geometry = "20x20")
img <- image_trim(img)
image_write(img, file.path('../rand/', paste0(format(cdate, '%Y%m%d'), '-', round(runif(1,min=0,max=100)), '.jpg')))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment