如何在Ruby中组合图像

Edu*_*ard 7 ruby linux shell ruby-on-rails image-processing

我有4个方形图像,1,2,3和4,每个有2048x2048px.

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

 1 2
 3 4
Run Code Online (Sandbox Code Playgroud)

现在我正在使用Gimp手动执行此操作,但处理量正在增加,我希望实现自动化解决方案.

在Ruby中有一个简单的方法吗?(Rails gem可以,或者可以从Rails应用程序内部运行的任何shell命令)

Gag*_*ami 6

尝试:'rmagick'宝石

require 'rmagick'

image_list = Magick::ImageList.new("image1.png", "image2.png", "image3.png")
image_list.write("combine.png")
Run Code Online (Sandbox Code Playgroud)

您也可以参考此SO问题,它与您的类似.


Edu*_*ard 5

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

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
Run Code Online (Sandbox Code Playgroud)