【问题标题】:How to combine images in Ruby如何在 Ruby 中合并图像
【发布时间】:2018-06-21 12:23:18
【问题描述】:

我有 4 张方形图片,1、2、3 和 4 张,每个图片的尺寸为 2048x2048 像素。

我需要将它们组合成一个 4096x4096px 的图像,如下所示:

 1 2
 3 4

现在我正在使用 Gimp 手动执行此操作,但处理量正在增长,我想实施自动化解决方案。

在 Ruby 中有没有简单的方法来做到这一点? (Rails gem 或者任何可以从 Rails 应用程序内部运行的 shell 命令都可以)

【问题讨论】:

  • 我会研究 rmagick,它是一个非常强大的图像处理器
  • 通过合并图像,您的意思是从 4 张图像中合成 1 张图像吗?试试 rmagick。

标签: ruby-on-rails ruby linux shell image-processing


【解决方案1】:

试试:'rmagick' gem

require 'rmagick'

image_list = Magick::ImageList.new("image1.png", "image2.png", "image3.png")
image_list.write("combine.png")

您也可以参考这个 SO Question,它与您的相似。

【讨论】:

  • 顺便说一句,rmagick gem 还需要您安装 ImageMagick,它会执行实际工作(rmagick 只是为 ImageMagick 提供了一个友好的 ruby​​ 界面)。安装 ImageMagick 传统上是一件很痛苦的事(无论如何我都这样做了),但现在我认为它更容易安装。
  • @MaxWilliams :是的,我同意你的看法。应该安装ImageMagick
  • 我试过了,但我得到了一张 2048x2048 的图像,每张图像相互重叠。我不希望它们在顶部,我需要第一个在顶部 0,0,第二个在顶部 0, 2048,第三个在顶部 2048, 0 和最后一个在顶部 2048, 2048。基本上,我想形成一个方形图像来自 4 个较小的方形图像,4 个中的每一个都是新图像的四分之一
【解决方案2】:

我标记了接受的答案,因为这是解决我的问题的起点。我还将在这里发布完整的工作解决方案:

require 'rmagick'
class Combiner
include Magick

  def self.combine
    #this will be the final image
    big_image = ImageList.new

    #this is an image containing first row of images
    first_row = ImageList.new
    #this is an image containing second row of images
    second_row = ImageList.new

    #adding images to the first row (Image.read returns an Array, this is why .first is needed)
    first_row.push(Image.read("1.png").first)
    first_row.push(Image.read("2.png").first)

    #adding first row to big image and specify that we want images in first row to be appended in a single image on the same row - argument false on append does that
    big_image.push (first_row.append(false))

    #same thing for second row
    second_row.push(Image.read("3.png").first)
    second_row.push(Image.read("4.jpg").first)
    big_image.push(second_row.append(false))

    #now we are saving the final image that is composed from 2 images by sepcify append with argument true meaning that each image will be on a separate row
    big_image.append(true).write("big_image.jpg")
  end
end

【讨论】:

    【解决方案3】:

    对于从 Google 登陆并可能使用更新版本的 MiniMagick (4.8.0)Rails (5.2.0) 的任何人,我可以使用以下代码水平组合图像:

    # Replace this with the path to the images you want to combine
    images = [
      "image1.jpg",
      "image2.jpg"
    ]
    
    processed_image = MiniMagick::Tool::Montage.new do |image|
      image.geometry "x700+0+0"
      image.tile "#{images.size}x1"
      images.each {|i| image << i}
      image << "output.jpg"
    end
    

    查看documentation 中的#geometry 选项以处理调整大小和放置。当前示例将图像大小调整为700px 高度,同时保持图像的纵横比。 +0+0 将放置图像,它们之间没有间隙。

    【讨论】:

    • 我还没有查看底层代码,但这可能比在 &amp;block 内执行 File.open 节省更多内存
    • 使用tile 选项来实现问题所需的布局。 image.tile "2x2"
    • 要将 proccessed_image 放入 blob,您需要在末尾添加 image.stdout
    【解决方案4】:

    您也可以尝试montage 方法。

    【讨论】:

      猜你喜欢
      • 2015-01-19
      • 2011-03-30
      • 1970-01-01
      • 2019-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-04
      相关资源
      最近更新 更多