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 crop
ping 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
Heroku: Pg::Connectionbad: Could Not Connect to Server: Connection Refused
What the Purpose of Bind/Unbind Methods in Ruby
Sending Http Post Request in Ruby by Net::Http
How to Send Mail with Ruby Over Smtp with Ssl (Not with Rails, No Tls for Gmail)
Using Rvm, But Can't Set Current Ruby Version (Ubuntu 11.10)
Generate a 'Link_To' to the Controller Action 'Edit', Dynamically
Heroku Rails Rake Task to Sync Production & Local Db
Ruby Array Concat Versus + Speed
Sorting an Array of Arrays in Ruby
Ruby Time.Parse Gives Me Out of Range Error
How to Use Factorygirl to Create an Attribute Called "Alias"
How to Remove All Characters in a String Until a Substring Is Matched, in Ruby
How to Use Ruby CSV Converters
Count Records Created Within the Last 7 Days
Ruby on Rails Collection Select - How to Pre-Select the Right Value
Ruby on Rails - Generating Bit.Ly Style Identifiers