Imagemagick: animated gif background flatten - transparency

I'm wondering how to remove and replace transparency in an animated GIF with ImageMagick.
My goal is to take several pages with different aspect ratios and make an animated GIF that cycles through them.
document1.png
I am using
convert -delay 50 -resize 212 -dispose Previous -page 212x275 -crop 212x275+0+0 +repage document1.png document2.png documents.gif 2>&1
to create the GIF.
This sizes my documents correctly, but leaves the empty space from the short images transparent, but I would like that to be white. How can I change the transparency to white?
doc.gif
I have tried
convert doc.gif -background black -alpha off doc2.gif
convert doc.gif -background black -flatten doc2.gif
-alpha doesn't seem to do anything, and -flatten removes the animation

Related

ImageMagick gif background not moving with png overlays

I am trying to change the background layer of a batch of png images to include a moving gif. I'm running on MacOs and imagemagick version 7.1.0-19.
There are two types of images I want to merge:
Overlay static images A*.png
Background image Moving#40.gif
The background gif takes 120 frames.
So far I have managed to build the images and create GIFs. However the animated gifs are not moving. I have tried including delay and loop but this does nothing. The code is as follows:
magick mogrify -format gif -delay 10 -draw 'image Dst_Over 0,0 0,0 "Moving#40.gif"' A*.png -loop 0
I have been looking for days for similar use cases but have not found a solution,I just recently got into ImageMagick so advice is much appreciated.
any ideas on how this can be accomplished?
One way would be to process them one at a time in a script loop, since one is a gif animation. Then you would use a null: separator and -layers composite. For example for one PNG.
Transparent PNG:
Animation:
magick logot.png null: \( glitter.gif -coalesce \) -gravity center -compose dstover -layers composite -layers optimize result1.gif
See https://legacy.imagemagick.org/Usage/anim_mods/#composite
Alternately, you can use -draw, but in the reverse with the png inside -draw and the GIF after magick.
magick glitter.gif -gravity center -draw 'image Over 0,0 0,0 "logot.png"' -layers optimize result2.gif

Set image inside png transparency

As the title suggests I have a PNG image that has some transparency. I'd like to fill that transparency with a second image (which is currently a JPEG, but it's not a problem to convert it to a PNG).
Every post I have found searching on the Internet was about the "inverse" problem (from an image with a background to an image with transparency), so obviously it did not work out for my situation; for example, I tried
convert -flatten myimg.png myimg.png
(taken from here) and
convert myimg1.png -transparent white myimg.png
(taken from here).
In ImageMagick 6, if the two images are the same size, then you can just flatten the transparent image over the background image.
Background (lena.jpg):
Transparent (logo_crop_trans.png):
convert lena.jpg logo_crop_trans.png -flatten lena_logo.jpg
If using ImageMagick 7, then change convert to magick.
If you want to anti-alias the transparent image so that it is not so jagged, then use some blur to smooth the outline (Unix syntax):
convert lena.jpg \( logo_crop_trans.png -channel a -blur 0x1 -level 50x100% +channel \) -compose over -composite lena_logo2.jpg
If on Windows remove the \ before the parentheses.

ImageMagick engraving effect on glass

I want to add logo on product image with engraving effect.
The original logo is
After adding it on the product it should look like this.
how to do this with imagemagick.
Using this as the trophy:
Then something along these lines:
convert trophy.jpg -gravity center \
\( G.png -colorspace gray -channel a -evaluate multiply 0.2 -resize 120x120 \) -composite result.png
So, I am basically loading the trophy, then in some "aside processing" in parentheses, loading the Google logo, converting it to greyscale, reducing the opacity by multiplying it by 0.2, resizing it and compositing it on top of the trophy.
By the way, if you were using GraphicsMagick, which doesn't have the parentheses I used to make sure I only convert the logo to greyscale and not the trophy, you would do it in a different order. First load the logo and process it (greyscale, resize etc), then load the trophy, then swap the order so the trophy goes to the background, like this:
gm convert G.png -colorspace gray -resize ... trophy.jpg -swap -composite result.png

ImageMagick rotate animated gif glitches

I'm using ImageMagick to rotate animated gifs. Simply:
convert image.gif -rotate 32 -alpha set -background none output.gif
Output:
https://s3-eu-west-1.amazonaws.com/uploads-eu.hipchat.com/108112/892631/ATp8mXXrDdSkCNu/sowa-test2.gif
Does anyone have a clue why output image is distorted this way and how to avoid this?
Without seeing the original image, I would suggest extracting each image, apply rotation, and then re-build the animated gif.
Example using the following gif:
convert anim_none.gif -scene 1 +adjoin tmp_%02d.gif
mogrify -rotate 32 -alpha set -background none tmp_*.gif
convert tmp_*.gif -loop 0 final.gif
And note: quality is expected to degrade with rotation operations.
The solution from emcconville didn't quite work for me. Part of the issue may have been that my layers were transparent and so not all the same dimensions. However, using his solution as a base and then modifying it based on things I saw elsewhere, I was able to get my gif rotated:
convert my_gif.gif -background transparent \
-virtual-pixel background -coalesce tmp_%02d.gif
mogrify -rotate -45 -background transparent -virtual-pixel background tmp_*.gif
convert tmp_*.gif -loop 0 my_gif_rotated.gif
Hopefully this helps others.

ImageMagick: How to change transparent background to a color?

Given an input image sample.i.png with a transparent background :
How to convert its background into a file sample.o.png with background rgb(160,160,255), opacity 100% (or 1).
Answer used:
convert source.png -background "rgb(160,160,255)" -flatten out.png
Please +1 emcconville's answer !
From Mark Random's suggestion. Create a background image with your color, and flatten the source image over it.
convert -size 150x150 xc:"rgb(160,160,255)" source.png -layers flatten out.png
Edit
If you don't want to worry about image size, just change the background directly.
convert source.png -background "rgb(160,160,255)" -flatten out.png
Add the -flatten to re-set the background color

Resources