composite gif files with RMagick

Discussion in 'Ruby' started by Wu Junchen, Apr 28, 2009.

  1. Wu Junchen

    Wu Junchen Guest

    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.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/.
     
    Wu Junchen, Apr 28, 2009
    #1
    1. Advertising

  2. Wu Junchen

    Tim Hunter Guest

    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.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/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


    --
    RMagick: http://rmagick.rubyforge.org/
     
    Tim Hunter, Apr 28, 2009
    #2
    1. Advertising

  3. Wu Junchen

    Wu Junchen Guest

    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.composite(gif2,5,5,CompositeOperator::OverCompositeOp) will
    add 5+39 and 5+13 offset over the page.
    --
    Posted via http://www.ruby-forum.com/.
     
    Wu Junchen, Apr 29, 2009
    #3
  4. Wu Junchen

    Tim Hunter Guest

    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.composite(gif2,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




    --
    RMagick: http://rmagick.rubyforge.org/
     
    Tim Hunter, Apr 30, 2009
    #4
  5. Wu Junchen

    Wu Junchen Guest

    Tim Hunter 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!
    --
    Posted via http://www.ruby-forum.com/.
     
    Wu Junchen, Apr 30, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hb
    Replies:
    6
    Views:
    9,164
  2. Beauregard T. Shagnasty

    Re: filename.gif or filename.gif.jpg?

    Beauregard T. Shagnasty, May 30, 2008, in forum: HTML
    Replies:
    1
    Views:
    776
    Jonathan N. Little
    May 30, 2008
  3. Bergamot
    Replies:
    0
    Views:
    466
    Bergamot
    May 30, 2008
  4. Iseng ---
    Replies:
    0
    Views:
    219
    Iseng ---
    Jul 22, 2006
  5. Pito Salas
    Replies:
    1
    Views:
    150
    Tim Hunter
    Apr 24, 2009
Loading...

Share This Page