Composite gif files with RMagick


#1

I have two gif files,and I want to composite them,there is my ruby code:

require ‘rmagick’
include Magick
gif1=ImageList.new(‘one.gif’)
gif2=ImageList.new(‘two.gif’)
dest=ImageList.new
0.upto(gif1.size-1){|i|
dest <<
gif1[i].composite(gif2[i%gif2.size],5,5,CompositeOperator::OverCompositeOp)
}
dest.write(‘test.gif’)

However,the size of the gif1 frames are not the same:
irb(main):092:0> gif1[0]
=> gif.gif=>0.gif GIF 128x128 128x128+0+0 PseudoClass 256c 8-bit 2kb
irb(main):093:0> gif1[1]
=> gif.gif=>1.gif GIF 71x73 128x128+29+13 PseudoClass 256c 8-bit 2kb

the xy-offset can just applied to the frame size,eg:71*73,so if I give
it -29 and -13,I still can’t get 128x128+0+0,how can I add all my gif2’s
frame to gif1 on 128x128+5+5?And how can I get the x-offset and y-offset
of the gif file,I can’t find some method to get the info ‘29+13’.

Any help will be appreciated!


#2

Wu Junchen wrote:

}
frame to gif1 on 128x128+5+5?And how can I get the x-offset and y-offset
of the gif file,I can’t find some method to get the info ‘29+13’.

Any help will be appreciated!

Attachments:
http://www.ruby-forum.com/attachment/3627/gifs.zip

I’m assuming that you want to composite a 71x73 image 5 pixels from the
top and 5 pixels from the left over a 128x128 image. Probably the page
offset (+29+13) is screwing you up.

You can remove the page offset by assigning a new Rectangle to the page
attribute. See
http://studio.imagemagick.org/RMagick/doc/imageattrs.html#page.
Something like

img.page = Rectangle.new(0,0,0,0)

should do the trick.

If you’re creating the smaller image by cropping a larger image, and you
don’t otherwise need the page offsets, you can prevent crop from
embedding the page offsets into the cropped image by supplying true as
the last argument to crop. See
http://studio.imagemagick.org/RMagick/doc/image1.html#crop.

Lastly, you can get the page offsets from the page attribute:

rect = img.page


#3

Tim H. wrote:

I’m assuming that you want to composite a 71x73 image 5 pixels from the
top and 5 pixels from the left over a 128x128 image. Probably the page
offset (+29+13) is screwing you up.

Thanks,I have an image in a gif frame,which size is 71x73,and it has a
128x128 page and 29+13 offset,I want to composite a 10X10 image 5 pixels
from the
top and 5 pixels from the left over the page,but not over the image in
the page,there is some way to do it?
gif1[i].composite(gif2[i],5,5,CompositeOperator::OverCompositeOp) will
add 5+39 and 5+13 offset over the page.


#4

Tim H. wrote:

require ‘RMagick’

page = Magick::Image.new(128, 128) {self.background_color = “black” }
back = Magick::Image.new(128, 128) {self.background_color = “cyan” }
back.crop!(29, 13, 71, 73)

page.composite!(back, Magick::CenterGravity, Magick::OverCompositeOp)

front = Magick::Image.new(10, 10) {self.background_color = “red” }
result = page.composite(front, 5, 5, Magick::OverCompositeOp)
p result
result.display

Thanks so much,that’s what I want!


#5

Wu Junchen wrote:

top and 5 pixels from the left over the page,but not over the image in
the page,there is some way to do it?
gif1[i].composite(gif2[i],5,5,CompositeOperator::OverCompositeOp) will
add 5+39 and 5+13 offset over the page.

When I composite a 10x10 image over a 71x73 image with a 128x128+29+13
page, I end up with a 71x73 image with a 128x128+29+13 image. That is,
the resulting image has the same size and page attribute.

If you want end up with a 128x128 image, you’ll have to start with one
and composite the smaller images over it. Something like this. You could
of course make the “page” image transparent. Here I’ve made it black so
you can see how the other two images are positioned.

require ‘RMagick’

page = Magick::Image.new(128, 128) {self.background_color = “black” }
back = Magick::Image.new(128, 128) {self.background_color = “cyan” }
back.crop!(29, 13, 71, 73)

page.composite!(back, Magick::CenterGravity, Magick::OverCompositeOp)

front = Magick::Image.new(10, 10) {self.background_color = “red” }
result = page.composite(front, 5, 5, Magick::OverCompositeOp)
p result
result.display