antuki
antuki

Reputation: 236

add background to a gif containing images of different sizes (R magick)

I would like to add an image background to a gif which contains images of different sizes like this beautiful christmas ball.

enter image description here

Here is the code i tried :

  #install.packages("magick")
  library(magick)
  setwd("C:/Users/...")
  christmas <- image_read('christmas.gif')
  background <- image_blank(250, 250, color = "red")
  frames <- image_composite(background, christmas, offset = "+0+0")
  animation <- image_animate(frames, fps = 5)
  image_write(animation, "output.gif", format="gif")

And here is the horrible result !!

enter image description here

The problem is that the 3 images of the ball gif are not of the same size so the images are moving when adding a background image.

      > christmas
      format width height colorspace matte filesize
      1    GIF   216    249       sRGB  TRUE    47656
      2    GIF   202    233       sRGB  TRUE    47656
      3    GIF   200    214       sRGB  TRUE    47656

I didn't find how to fix this problem... : If you could help me before christmas it would be great !!!

Thanks !

Upvotes: 1

Views: 611

Answers (1)

fmw42
fmw42

Reputation: 53154

Your problem is likely that you need to coalesce the frames to fill them out to the same size. Then later optimize them again. In ImageMagick, I would do:

convert ornament.gif -coalesce -background red -alpha background -alpha off -layers optimize ornament2.gif

enter image description here

See

http://www.imagemagick.org/script/command-line-options.php#coalesce http://www.imagemagick.org/Usage/masking/#alpha http://www.imagemagick.org/Usage/anim_opt/#optimize

Sorry I do not know the equivalent commands in RMagick. But they likely have similar names. Hopefully, this will get you pointed in the right direction.

Upvotes: 1

Related Questions