How to combine multiple images into one in Ruby | Inkoop Blog

We use mini_magick gem to combine multiple images of different sizes into one. We crop and resize the images to make it fit for merging and then combine them using mini_magick.

Posted by Ameena on 30 May 2017

This post includes maximum of combining four images into one image in Ruby. You can do as many as you want using the same logic.

The main logic is

  • Crop the image.
  • Resize the image.

Consider the below four images as an example:

Image1

Image2

Image3

Image4

Case1: Combine two images into one image

#image_generator.rb

require 'mini_magick'
images = ['path/to/image1', 'path/to/image2', 'path/to/image3', 'path/to/image4']

def generate_random_file_name
  "#{SecureRandom.hex(8)}.png"
end

def crop_and_resize image, aspect_ratio=[16, 9], size="320x180"
  # make a copy of the image to crop and resize
  cloned_image = generate_random_file_name
  FileUtils.cp(image, cloned_image)
  image = MiniMagick::Image.new(cloned_image)
  aspect_ratio_width = aspect_ratio[0]
  aspect_ratio_height = aspect_ratio[1]
  image_width = image.width
  image_height = (image_width * aspect_ratio_height) / aspect_ratio_width
  if image_height > image.height
    image_height = image.height
    image_width = (image_height * aspect_ratio_width ) / aspect_ratio_height
  end
  image.crop "#{image_width}x#{image_height}+0+0"
  image.resize size
  image.path
end

def process_for_2 images
  image_1 = crop_and_resize(images[0], [8, 9], "160x180")
  image_2 = crop_and_resize(images[1], [8, 9], "160x180")
  final_file_path = 'path/to/final_image'
  system "convert #{image_1} #{image_2} +append #{final_file_path}"
end

process_for_2 images

The resulting image is:

final_file_path

Case2: Combine three images into one image

#image_generator.rb

...
def process_for_3 images
  image_1 = crop_and_resize(images[0], [16, 9], "160x90")
  image_2 = crop_and_resize(images[1], [16, 9], "160x90")
  image_1_and_image_2_combined = 'path/to/image_1_and_image_2_combined'
  system "convert #{image_1} #{image_2} -append #{image_1_and_image_2_combined}"
  image_3 = crop_and_resize(images[2], [8, 9], "160x180")
  final_file_path = 'path/to/final_image'
  system "convert #{image_1_and_image_2_combined} #{image_3} +append #{final_file_path}"
end

process_for_3 images

Resulting Image:

final_file_path

Case3: Combine four images into one image

#image_generator.rb

...
def process_for_4 images
  image_1 = crop_and_resize(images[0], [16, 9], "160x90")
  image_2 = crop_and_resize(images[1], [16, 9], "160x90")
  image_1_and_image_2_combined = 'path/to/image_1_and_image_2_combined'
  system "convert #{image_1} #{image_2} -append #{image_1_and_image_2_combined}"
  image_3 = crop_and_resize(images[2], [16, 9], "160x90")
  image_4 = crop_and_resize(images[3], [16, 9], "160x90")
  image_3_and_image_4_combined = 'path/to/image_3_and_image_4_combined'
  system "convert #{image_3} #{image_4} -append #{image_3_and_image_4_combined}"
  final_file_path = 'path/to/final_image'
  system "convert #{image_1_and_image_2_combined} #{image_3_and_image_4_combined} +append #{final_file_path}"
end

process_for_4 images

Resulting Image:

final_file_path

keep Coding !!!

Ameena


Have a Project in mind?