【问题标题】:How can I use the magick package in R to add a feather around the edge of an image?如何使用 R 中的 magick 包在图像边缘添加羽毛?
【发布时间】:2019-05-22 16:29:57
【问题描述】:

我有一个使用 magick 包读入 R 的 gif 文件,我想对每帧的图像边缘进行模糊处理或添加羽化效果。我从文档中看到这在 ImageMagick 中是可能的(例如 https://www.imagemagick.org/Usage/morphology/#distance_featherhttps://www.imagemagick.org/Usage/transform/#vignette),但无法弄清楚如何将此信息转换为 R。我需要使用哪些魔法函数和参数来制作这会发生吗?

我知道我需要单独处理 gif 的每一帧,例如:

img <- image_read("myGif.gif")

featheredGif <- _____ # ????insert a function to feather edges, applied to img[1]

for(i in 2:length(img)){
  featheredFrame <- _____ # ???? insert function to feather edges, applied to img[i]
  featheredGif <- c(featheredGif, featheredFrame)
}

我希望结果看起来类似于此https://www.imagemagick.org/Usage/thumbnails/soft_edge.png 或此https://www.imagemagick.org/Usage/morphology/rose_feathered.png,但应用于 gif 中的每一帧。

【问题讨论】:

    标签: r imagemagick


    【解决方案1】:

    抱歉,我不知道 RMagick 或 R。但这里有用于绘制椭圆羽化和圆形矩形羽化的 ImageMagick CLI 命令。

    输入:

    椭圆形:

    Read the image
    Clone it and use -vignette to draw an ellipse and extract the alpha channel, blur it, then apply -level
    Then put the result into the alpha channel of the input image
    
    convert thumbnail.gif \
    \( -clone 0 -background none -virtual-pixel none -vignette 0x0+0+0 \
    -alpha extract -blur 0x10 -level 50x100% \) \
    -alpha off -compose copyopacity -composite \
    result.png
    


    圆角矩形:

    Read the input image and get the bottom right corner coordinate   
    Read it again. 
    Clone it, fill it with black, draw a round rectangle, blur it, apply -level
    Then put the result into the alpha channel of the input image
    
    wm1=`convert thumbnail.gif -format "%[fx:w-1]" info:`
    hm1=`convert thumbnail.gif -format "%[fx:h-1]" info:`
    convert thumbnail.gif \
    \( -clone 0 -fill black -colorize 100 \
    -fill white -virtual-pixel black -draw "roundrectangle 0,0 $wm1,$hm1 15,15" \
    -blur 0x10 -level 50x100% \) \
    -alpha off -compose CopyOpacity -composite \
    result2.png
    


    所以你需要 RMagick 的 -vignette、-composite、-draw、-blur 和 -level 等价物。

    【讨论】:

    • 感谢您在 ImageMagick 中概述如何执行此操作。我也希望有人能帮我把它翻译成 R。
    猜你喜欢
    • 2019-09-21
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多