# Composite gif files with RMagick

**URL:** <https://www.ruby-forum.com/t/composite-gif-files-with-rmagick/166677>\
**Category:** Ruby\
**Created:** [April 28, 2009, 2:40pm UTC](https://www.ruby-forum.com/t/composite-gif-files-with-rmagick/166677 "2009-04-28T14:40:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![phoenix520](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/6f9a4e/32.png) [@phoenix520](https://www.ruby-forum.com/u/phoenix520)\
**Post date:** [April 28, 2009, 2:40pm UTC](https://www.ruby-forum.com/t/composite-gif-files-with-rmagick/166677/1 "2009-04-28T14:40:10Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![phoenix520](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/6f9a4e/32.png) [@phoenix520](https://www.ruby-forum.com/u/phoenix520)\
**Post date:** [April 29, 2009, 12:26am UTC](https://www.ruby-forum.com/t/composite-gif-files-with-rmagick/166677/2 "2009-04-29T00:26:06Z")

</div>

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](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](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](http://studio.imagemagick.org/RMagick/doc/image1.html#crop).

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

rect = img.page

---

<div class="post-metadata">

**Author:** ![phoenix520](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/6f9a4e/32.png) [@phoenix520](https://www.ruby-forum.com/u/phoenix520)\
**Post date:** [April 29, 2009, 6:14am UTC](https://www.ruby-forum.com/t/composite-gif-files-with-rmagick/166677/3 "2009-04-29T06:14:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![phoenix520](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/6f9a4e/32.png) [@phoenix520](https://www.ruby-forum.com/u/phoenix520)\
**Post date:** [April 30, 2009, 3:50am UTC](https://www.ruby-forum.com/t/composite-gif-files-with-rmagick/166677/4 "2009-04-30T03:50:05Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![phoenix520](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/6f9a4e/32.png) [@phoenix520](https://www.ruby-forum.com/u/phoenix520)\
**Post date:** [April 30, 2009, 3:42am UTC](https://www.ruby-forum.com/t/composite-gif-files-with-rmagick/166677/5 "2009-04-30T03:42:45Z")

</div>

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
