目的是为了
我有 1 个工作,但不确定这是最好的方法。这是一个工作示例。
library(magick)
path <- "https://cdn.pixabay.com/photo/2016/08/17/21/12/people-1601516_960_720.jpg"
img <- magick::image_read(path)
img
原图ii <- magick::image_info(img)
ii_min <- min(ii$width,ii$height)
img1 <- magick::image_crop(img, geometry=paste0(ii_min,"x",ii_min,"+0+0"),repage=TRUE)
img1
裁剪的方形纵横比我不知道如何让最后一部分 (2) 在 R 中工作。虽然我已经设法在 unix 中使用 image-magick 让它工作。
convert -size 500x500 xc:white -fill cropped.jpeg -draw "circle 250,250 250,1" circ.jpg
圆形框架我正在寻找 R 中 2 的解决方案。
最佳答案
library(magick)
path <- "https://cdn.pixabay.com/photo/2016/08/17/21/12/people-1601516_960_720.jpg"
im <- magick::image_read(path)
# get height, width and crop longer side to match shorter side
ii <- magick::image_info(im)
ii_min <- min(ii$width, ii$height)
im1 <- magick::image_crop(im, geometry=paste0(ii_min, "x", ii_min, "+0+0"), repage=TRUE)
# create a new image with white background and black circle
fig <- magick::image_draw(image_blank(ii_min, ii_min))
symbols(ii_min/2, ii_min/2, circles=(ii_min/2)-3, bg='black', inches=FALSE, add=TRUE)
dev.off()
# create an image composite using both images
im2 <- magick::image_composite(im1, fig, operator='copyopacity')
# set background as white
magick::image_background(im2, 'white')
R version 4.0.0
magick_2.5.2
关于R 魔法 : Square crop and circular mask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64597525/