如何在 Ruby 中合并图像
How to combine images in Ruby
我有 4 张正方形图片,1、2、3 和 4,每张 2048x2048 像素。
我需要将它们组合成一张 4096x4096 像素的图像,如下所示:
1 2
3 4
现在我正在使用 Gimp 手动执行此操作,但是处理量越来越大,我想实施一个自动化解决方案。
在 Ruby 中有没有简单的方法来做到这一点? (Rails gem 可以,或者任何 shell 命令都可以 运行 从 Rails 应用程序内部)
require 'rmagick'
image_list = Magick::ImageList.new("image1.png", "image2.png", "image3.png")
image_list.write("combine.png")
你也可以参考这个SO Question它和你的差不多
我标记了已接受的答案,因为这是解决我问题的起点。我还将在此处 post 完整的工作解决方案:
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
您也可以尝试 montage 方法。
对于从 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
将放置图像,它们之间没有间隙。
我有 4 张正方形图片,1、2、3 和 4,每张 2048x2048 像素。
我需要将它们组合成一张 4096x4096 像素的图像,如下所示:
1 2
3 4
现在我正在使用 Gimp 手动执行此操作,但是处理量越来越大,我想实施一个自动化解决方案。
在 Ruby 中有没有简单的方法来做到这一点? (Rails gem 可以,或者任何 shell 命令都可以 运行 从 Rails 应用程序内部)
require 'rmagick'
image_list = Magick::ImageList.new("image1.png", "image2.png", "image3.png")
image_list.write("combine.png")
你也可以参考这个SO Question它和你的差不多
我标记了已接受的答案,因为这是解决我问题的起点。我还将在此处 post 完整的工作解决方案:
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
您也可以尝试 montage 方法。
对于从 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
将放置图像,它们之间没有间隙。