【问题标题】:Add a rectangle to the image using image magic使用图像魔术将矩形添加到图像
【发布时间】:2012-07-13 08:25:19
【问题描述】:

美好的一天。 如何在下图中的 logo.jpg 上加上 white_rectangle.jpg 使用 Imagemagic。
还有一个额外的问题:Ruby 的方法可以完成这项任务。

def (path_to_image)
# impose white_rectangle.jpg on logo
end

【问题讨论】:

    标签: ruby imagemagick


    【解决方案1】:

    这可以使用RMagick 轻松完成:

    require 'RMagick'
    
    logo = Magick::Image.read("logo.jpg").first
    rect = Magick::Image.read("white_rectangle.jpg").first
    result = logo.composite(rect, x, y, Magick::CopyCompositeOp)
    result.write "result.jpg"
    

    另一种方法是只绘制一个白色矩形而不使用合成图像:

    image = Magick::Image.read("logo.jpg").first
    gc = Magick::Draw.new
    gc.stroke = 'white'
    gc.fill = 'white'
    gc.rectangle x_start, y_start, x_end, y_end
    gc.draw(image)
    image.write "result.jpg"
    

    使用 ImageMagick 命令行工具,您可以像这样将一张图像与另一张图像叠加:

    $ composite white_rectangle.jpg logo.jpg -geometry +x+y result.jpg
    

    【讨论】:

    • 应该为我附加的图像设置哪些参数(rect、x、y)?
    • rect 是在 white_rectangle.jpg 中保存实际图像的对象。对于这个职位,只需尝试,直到你做对为止。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    • 1970-01-01
    相关资源
    最近更新 更多