【问题标题】:Cut circle out of image with RMagick使用 RMagick 从图像中切出圆圈
【发布时间】:2012-06-29 08:07:12
【问题描述】:

我想使用 rmagick 从图像中剪出一个圆圈。

这是我希望能够完成的示例:

-->

似乎我想使用http://studio.imagemagick.org/RMagick/doc/draw.html#circle 来切割一个圆圈,然后使用 clip_path 来掩盖它,但是文档不是很清楚。有人能指出我正确的方向吗?

【问题讨论】:

    标签: ruby image-processing imagemagick rmagick


    【解决方案1】:

    这就是我使用 Imagemagick 和 php 的方式:

    // Canvas the same size as the final image
    exec("convert -size 800x533 xc:white white.jpg");
    // The mask 
    exec("convert -size 800x533 xc:none -draw \"fill black circle 400,265 400,50\"  write_mask.png");
    // Cut the whole out of the canvas  
    exec("composite -compose Dst_Out write_mask.png white.jpg -matte step.png");
    // Put the canvas over the image and trim off excess white background   
    exec("convert IMG_5745.jpg  step.png -composite -trim final.jpg");
    

    您应该能够按照流程进行操作吗?

    之后清理临时图像 - 我倾向于将临时图像保存为 .miff 格式,然后编写一个循环来删除所有 .miff 图像。或者只保留它们,如果您对临时图像使用相同的名称,则每次运行代码时它们都会被覆盖。

    【讨论】:

      【解决方案2】:
      require 'rmagick'
      
      im = Magick::Image.read('walter.jpg').first
      
      circle = Magick::Image.new 200, 200
      gc = Magick::Draw.new
      gc.fill 'black'
      gc.circle 100, 100, 100, 1
      gc.draw circle
      
      mask = circle.blur_image(0,1).negate
      
      mask.matte = false
      im.matte = true
      im.composite!(mask, Magick::CenterGravity, Magick::CopyOpacityCompositeOp)
      
      im.write 'walter_circle.png'
      

      【讨论】:

      • 你会如何处理两张图片?不是圆形,而是透明图片
      • 此解决方案不适用于带有 alpha 通道的 png 图像
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-09
      • 2015-06-13
      • 1970-01-01
      • 2011-06-06
      相关资源
      最近更新 更多