Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > composite gif files with RMagick

Reply
Thread Tools

composite gif files with RMagick

 
 
Wu Junchen
Guest
Posts: n/a
 
      04-28-2009
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!

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

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Tim Hunter
Guest
Posts: n/a
 
      04-28-2009
Wu Junchen wrote:
> 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!
>
> 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/RMagic...ttrs.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/RMagic...age1.html#crop.

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

rect = img.page


--
RMagick: http://rmagick.rubyforge.org/

 
Reply With Quote
 
 
 
 
Wu Junchen
Guest
Posts: n/a
 
      04-29-2009
Tim Hunter 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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Tim Hunter
Guest
Posts: n/a
 
      04-30-2009
Wu Junchen wrote:
> Tim Hunter 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.


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, 12 {self.background_color = "black" }
back = Magick::Image.new(128, 12 {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




--
RMagick: http://rmagick.rubyforge.org/

 
Reply With Quote
 
Wu Junchen
Guest
Posts: n/a
 
      04-30-2009
Tim Hunter wrote:

>
> require 'RMagick'
>
> page = Magick::Image.new(128, 12 {self.background_color = "black" }
> back = Magick::Image.new(128, 12 {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!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between gem install rmagick and port rb-rmagick Pito Salas Ruby 1 04-24-2009 09:29 PM
RMagick error: " no such file to load -- RMagick" jeljer te Wies Ruby 2 03-23-2009 12:23 AM
Re: filename.gif or filename.gif.jpg? Beauregard T. Shagnasty HTML 1 05-30-2008 01:23 PM
rmagick: animated gif resize returns corrupted Iseng --- Ruby 0 07-22-2006 10:20 AM
How to convert a base 64 encoded GIF image to a *.gif file? hb ASP .Net 6 09-28-2004 08:24 PM



Advertisments