R 魔法 : Square crop and circular mask

标签 r image-processing imagemagick rmagick

目的是为了

  • 将任何输入图像转换为方形纵横比和
  • 添加圆形蒙版并用白色或透明填充外部。

  • 我有 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
    
    原图
    enter image description here
    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
    
    裁剪的方形纵横比
    enter image description here
    我不知道如何让最后一部分 (2) 在 R 中工作。虽然我已经设法在 unix 中使用 image-magick 让它工作。
    convert -size 500x500 xc:white -fill cropped.jpeg -draw "circle 250,250 250,1" circ.jpg
    
    圆形框架
    enter image description here
    我正在寻找 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')
    
    enter image description here
    R version 4.0.0
    magick_2.5.2
    

    关于R 魔法 : Square crop and circular mask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64597525/

    相关文章:

    r - 使用 Rcpp 就地编辑后 sum 函数出现问题

    r - $$ 在 R Markdown LaTeX 中被弃用了吗?

    r - 为矩阵向量化 min()

    python - 查找图像内的边缘(矩形的边界)

    php - 如何在 PHP 中使用 Imagemagick 仅修剪图像的左侧和右侧?

    r - ggplot 的 scale_y_log10 行为

    c++ - opencv中拜耳模式代码的含义是什么

    image-processing - 如何消除此视频序列中的噪音?

    python - 用pytesseract读取低分辨率图像

    windows - 在 Windows 上使用 ImageMagick 并排附加两个 gif