Creating a collage from a collection of images in Ruby - ruby

I have a collection of images that have been laid out in a rectangle to look like a collage. How can I take those images and create a single image out of them in Ruby?
For example I have three images that I want placed in the image as follows:
Image 1: (0,0) - (300,400)
Image 2: (350, 0) - (500, 200)
Image 3: (350, 220) - (500, 400)

You can try something like this with RMagick:
require 'RMagick'
bg = Image.read('bg.png') # may be a background image...
image1 = Image.read('image1.png')
image2 = Image.read('image2.png')
image3 = Image.read('image3.png')
bg.composite!(image1, 0, 0, OverCompositeOp)
bg.composite!(image2, 350, 0, OverCompositeOp)
bg.composite!(image3, 350, 220, OverCompositeOp)
bg.write('collage.png')

you probably want to use an image library like RMagick ...
http://www.imagemagick.org/RMagick/doc/

Related

How to add a background image to a button in pygame gui?

I am trying to add an image as a background of a button in pygame gui. I have the button created now and I just need to add the image. How can I add the image to the button?
This is my code
game1 = pygame.Rect(150 , 100, 200, 150) # creates a rect object
pygame.draw.rect(screen, [255, 100, 0], game1) # draw objects down here
it works fine
You're probably looking for pygame.Surface.blit (http://www.pygame.org/docs/ref/surface.html#pygame.Surface.blit)
Load your background image and blit it wherever you want.
img = pygame.image.load('thisisanimage.png')
WhateverYourDisplayNameIs.blit(img,(x,y))
Just in case you're confused, it looks like you're using screen as your display surface.

Append Images to PNG via Rmagick

for add text to image I'm doing next-
canvas = Magick::Image.read("init.png").first
gc = Magick::Draw.new
gc.pointsize(12)
gc.text(5, 207, params['property_type'])
gc.draw(canvas)
canvas.write("#tst.png")
How I can add images to exist picture(imposed from above)?
I found solution.
canvas = Magick::Image.read("init.png").first
append_image = Magick::Image.read('another.png').first
canvas.composite!(append_image, 0, 0, Magick::OverCompositeOp)

Composite manipulated images with RMagick

I'm trying to create a composite image from two original images, a background and an overlay which I manipulate using RMagick like follows:
background = ImageList.new("foo.png")
overlay_original = ImageList.new("bar.png")
overlay_resized = overlay_original.resize_to_fit(400,400)
overlay_cropped = overlay_resized.crop(NorthWestGravity, 400, 200)
new_image = ImageList.new
new_image = new_image.composite_layers(background)
new_image = new_image.composite_layers(overlay_cropped)
When I do this it gives me the following error: ArgumentError: no images in this image list
When I just try to check the length of the background and overlay_cropped image lists (background.length and overlay_cropped.length) it tells me the background image list has 1 image, but that overlay_cropped has no images:
NoMethodError: undefined method length' for bar.png PNG 640x1096=>400x200 400x400+0+0 DirectClass 8-bit:Magick::Image
Any ideas on what I'm doing wrong? I"m guessing the answer is pretty obvious.
Issue resolved by changing it to the following:
marketing_image = marketing_image.composite(background, NorthWestGravity, 0, 0, OverCompositeOp)
marketing_image = marketing_image.composite(overlay_cropped, NorthWestGravity, 327, 126, OverCompositeOp)

Rmagick setting opacity in watermark with transparency

I am trying to create a watermark with different opacity values (from 0 opaque value to 1 totally transparent).
I have the following method for RMagick in ruby:
# 0 = opaque (Magick::OpaqueOpacity) 1= transparent (Magick::TransparentOpacity)
def watermark(opacity = 0.99, size = 'm')
manipulate! do |img|
logo = Magick::Image.read("#{Rails.root}/app/assets/images/watermark#{size}.png").first
logo.alpha(Magick::ActivateAlphaChannel)
logo.opacity = (1 - opacity.to_f) * Magick::QuantumRange
img.alpha(Magick::ActivateAlphaChannel)
img = img.composite(logo, Magick::NorthWestGravity, 0, 0, Magick::OverCompositeOp)
end
end
My problem is that it seems to work, but the composite mode or the alpha composite or setting the opacity or alpha is failing, because I get a black transparency in the image. For example if my watermark is a totally transparent image with a text, that I put over a car image, then I get a more dark or nightly image with the watermark, so the background of the watermark it is not blending properly.
Any suggestions to set properly the opacity in the watermark image? Maybe some method to disolve the watermark?
EDIT: Adding image examples:
http://uppix.com/f-watermarkg53925b100016ab8e.png (watermark)
http://oi62.tinypic.com/2us8rxl.jpg (base image)
http://oi60.tinypic.com/2pt6mg3.jpg (composition)
Thanks to Neil Slater, I finally found the right solution. I need a combination of composite operation of DstIn + Over in my finalt result:
def watermark(opacity = 0.99, size = 'm')
manipulate! do |img|
logo = Magick::Image.read("#{Rails.root}/app/assets/images/watermark#{size}.png").first
logo.alpha(Magick::ActivateAlphaChannel)
white_canvas = Magick::Image.new(logo.columns, logo.rows) { self.background_color = "none" }
white_canvas.alpha(Magick::ActivateAlphaChannel)
white_canvas.opacity = Magick::QuantumRange - (Magick::QuantumRange * opacity)
# Important: DstIn composite operation (white canvas + watermark)
logo_opacity = logo.composite(white_canvas, Magick::NorthWestGravity, 0, 0, Magick::DstInCompositeOp)
logo_opacity.alpha(Magick::ActivateAlphaChannel)
# Important: Over composite operation (original image + white canvas watermarked)
img = img.composite(logo_opacity, Magick::NorthWestGravity, 0, 0, Magick::OverCompositeOp)
end
end

How can I crop an image in Qt?

I load a PNG image in a QPixmap/QImage and I want to crop it. Is there a function that does that in Qt, or how should I do it otherwise?
You can use QPixmap::copy:
QRect rect(10, 20, 30, 40);
QPixmap original('image.png');
QPixmap cropped = original.copy(rect);
There is also QImage::copy:
QRect rect(10, 20, 30, 40);
QImage original('image.png');
QImage cropped = original.copy(rect);
Use QImage instead of QPixmap:
QImage image("initial_image.jpg");
QImage copy ;
copy = image.copy( 0, 0, 128, 128);
copy.save("cropped_image.jpg");
This code will save a file cropped to upper left corner 128x128px.
Since you use QPixmap, you can use its copy method and supply it with a QRect to perform the actual crop.
Just use of the QPixmap's copy() functions.
This text is result of reading the first comment on your quiestion:
Sometimes it is better to wrap around an image. That is to have an image that is part of another image or in other words points to a part of another image. This is way the wrapped image does not require additional memory, except for its header. You can display or save the wrapped image without worries. The downside is that the original image must remain valid until you use the wrapped image, also if you are drawing in the wrapped image it will affect the source.

Resources