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)
Related
I want to crop images in R. I use this code for my data, but I get error. It does not make sense because there is no img.crop: I found that the class of image is "cimg". Do you think I need to change it to array class? Do you know how?
## example where you know where to crop the image
img <- load.image('C:/image/17.jpg')
plot(img)
dim(img)
print(k)
img22 <- crop.image(img ,xleft=446,ybottom=7,xright=203,ytop=256)
split.screen(c(1,2))
screen(1)
image2(img,asp=1,main='Original')
screen(2)
image2(img22[[1]],asp=1,main='Cropped')
class(img)
Error in crop.image(img, xleft = 446, ybottom = 7, xright = 203, ytop = 256) :
object 'img.crop' not found
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)
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
Good day.
How to impose white_rectangle.jpg on logo.jpg in the image below
using Imagemagic.
And a bonus question: what's Ruby's method can make the task.
def (path_to_image)
# impose white_rectangle.jpg on logo
end
This can easily be accomplished using RMagick:
require 'RMagick'
logo = Magick::Image.read("logo.jpg").first
rect = Magick::Image.read("white_rectangle.jpg").first
result = logo.composite(rect, x, y, Magick::CopyCompositeOp)
result.write "result.jpg"
An alternative is to just draw a white rectangle without using a composite image:
image = Magick::Image.read("logo.jpg").first
gc = Magick::Draw.new
gc.stroke = 'white'
gc.fill = 'white'
gc.rectangle x_start, y_start, x_end, y_end
gc.draw(image)
image.write "result.jpg"
Using ImageMagick command line tools, you can overlay one image with another like this:
$ composite white_rectangle.jpg logo.jpg -geometry +x+y result.jpg
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/