Combine Thumbnails to One Large Image with Rmagick

Combine Thumbnails to One Large Image with RMagick

Assuming all the images are in the current directory and named from 1.jpg to n.jpg and row * col = n.

include Magick
row = NUM_ROWS
col = NUM_COLS
ilg = ImageList.new
1.upto(col) {|x| il = ImageList.new
1.upto(row) {|y| il.push(Image.read((y + (x-1)*col).to_s + ".jpg").first)}
ilg.push(il.append(false))}
ilg.append(true).write("out.jpg")

How to combine images in Ruby

Try: 'rmagick' gem

require 'rmagick'

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

You can also refer this SO Question it's similar to yours.

Merge Images Side by Side (Horizontally)

ImageMagick ships with the montage utility. Montage will append each image side-by-side allowing you to adjust spacing between each image (-geometry), and the general layout (-tile).

montage [0-4].png -tile 5x1 -geometry +0+0 out.png

Other examples can be found on Montage Usage page

Rmagick resize and flatten_image error

I found the solution to the problem Rmagick requires the images when they are resized that they should fit into a square. So the resize_to_fit function works correctly when both dimensions are the same and are the larger one among height or width.

So I changed the code to do the following

        if height > width
images[1]=images[1].resize_to_fit!(height, height)
else
images[1]=images[1].resize_to_fit!(width, width)
end

Cut one image into smaller pieces

How about cropping the input image once for each of the output images:

output_images =
(0...10).collect { |i|
input_image.crop(0, i*100, 3000, 100, true)
}

More generally (but still assuming the Y resolution is divisible by the number of slices):

def slice_image_horizontally(image, num_slices)
slice_height = image.y_resolution / num_slices
(0...num_slices).collect { |i|
image.crop(
0, i * slice_height,
image.x_resolution, slice_height,
true # reset image offset
)
}
end

http://www.imagemagick.org/RMagick/doc/image1.html#crop

RMagick Rounded Corners

If you are using paperclip, check http://loo.no/articles/rounded-corners-with-paperclip

How do I combine a number of images into a single PDF using ruby?

You can use RMagick's ImageList#write:

If the image format indicated by the filename supports multiple images per file (animated images), write writes all the images in the imagelist to a single file.

Example:

require 'rmagick'

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

Take center part of image while resizing

I would approach this as follows:

  • Resize image to a 180x180 square
  • Remove (180-140)/2 from the top
  • Remove (180-140)/2 from the bottom

Something like this should do it:

def merge
manipulate! do |img|
img.resize '180x180' # resize to 180px square
img.shave '0x20' # Removes 20px from top and bottom edges

img # Returned image should be 180x140, cropped from the centre
end
end

Of course, this assumes your input image is always a square. If it wasn't square and you've got your heart set on the 180x140 ratio, you could do something like this:

def merge
manipulate! do |img|
if img[:width] <= img[:height]
# Image is tall ...
img.resize '180' # resize to 180px wide
pixels_to_remove = ((img[:height] - 140)/2).round # calculate amount to remove
img.shave "0x#{pixels_to_remove}" # shave off the top and bottom
else
# Image is wide
img.resize 'x140' # resize to 140px high
pixels_to_remove = ((img[:width] - 180)/2).round # calculate amount to remove
img.shave "#{pixels_to_remove}x0" # shave off the sides
end

img # Returned image should be 180x140, cropped from the centre
end
end


Related Topics



Leave a reply



Submit