Superimpose a transparent PNG on a GIF animation - imagemagick

I want to add watermark images to GIF animations with ImageMagick.
The following doesn't work: it just gives produces a weird single image with crazy colors, and the transparent-watermark.png doesn't seem to be there at all.
composite -compose Dst_Over background-gif.gif transparent-watermark.png final.gif
How can this be accomplished? It's similar to annotating but with an image instead of text.

convert background-animation.gif -coalesce -gravity NorthEast -draw 'image over 0,0 0,0 "transparent-watermark.png"' -layers Optimize final.gif

I've had same problem with
convert -delay 4 -loop 0 *.png animated.gif
And fix it adding -alpha remove
convert -delay 4 -loop 0 *.png -alpha remove animated.gif
Try -alpha remove or -background white(other cases) to fix it.

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

How to split an image with a grid and preserve transparency bounding box

I have some png images that I want to split it into parts, like by grid or size.
But each part should have the same bounding box (transparency) as original image.
Example:
Splitting image into 2 parts.
Original: 200 × 89
Output:
part_1.png, 200 × 89
part_2.png, 200 × 89
Can ImageMagick do this? Or any other app or method.
My actual goal is to split into 100+ slices images.
EDIT:
Another goal to have an indents for each slice. Say indent = 10px.
Example:
Input: 200 x 100
Output:
part_1.png, 200 x 100
part_2.png, 200 x 100
And just as example, to visually compare input and output: combined output images in Photoshop as layer added one onto another
200 x 100 :
Also this is showing input image added onto combined(so it's better to see what was cropped and how):
In ImageMagick, you can split an image into many parts with the -crop command. For your example above with two parts, you can do that with the following commands. ImageMagick will append -0, -1 ... to the output file names.
ImageMagick 6:
dim=`convert image.png -format "%wx%h" info:`
convert \( -size $dim xc:none \) null: \( image.png -crop 50x100% \) -layers composite result.png
ImageMagick 7:
magick \( image.png -set option:dim "%wx%h" -crop 50x100% \) null: \( -size "%[dim]" xc:none \) -reverse -layers composite result.png
The results are:
See
http://www.imagemagick.org/Usage/crop/#crop
http://www.imagemagick.org/Usage/crop/#crop_percent
http://www.imagemagick.org/Usage/crop/#crop_tile
http://www.imagemagick.org/Usage/crop/#crop_quad
http://www.imagemagick.org/Usage/crop/#crop_equal
http://www.imagemagick.org/script/command-line-options.php#layers
Note that -crop keeps the virtual canvas information if you do not add +repage afterwards. So to put the individual images back into their original placement, you have to composite them onto a transparent background the size of the input. That is done in one command using -layers composite using the null: separator.
Here is another way to add transparent areas between parts of a crop in ImageMagick. Crop the image into pieces, chop off the parts you want to remove, then pipe to montage to add the spacing back.
Input:
Here I make this into a 4x4 grid of images with 10 pixel spacing:
convert lena.png -crop 25%x25% +repage -gravity east -chop 10x0 -gravity south -chop 0x10 +repage miff:- | montage - -background none -tile 4x4 -geometry +5+5 result.png
To answer your new question, you can do that with a script loop. On a Unix-like platform, assuming your images do not have spaces, you can do the following:
cd path/to/current_folder
list=`ls *.png`
for img in $list; do
name=`convert $img -format "%t" info:`
dim=`convert $img -format "%wx%h" info:`
convert \( -size $dim xc:none \) null: \( $img -crop 50x100% \) -layers composite -scene 1 path/to/new_folder/${name}_%d.png
done
If you want leading 0s in the output, say 3, use path/to/new_folder/${name}_%03d.png.
Note that to start with 1 rather than 0, I have added -scene 1.
Sorry, I do not know how to script for Windows.
Please always provide your ImageMagick version and platform.
In ImageMagick, the best way to put transparent areas into your image is with a binary mask that is put into the alpha channel of your image.
convert input.png \( -size 200x89 xc:white -size 10x89 xc:black -gravity center -composite \) -alpha off -compose copy_opacity -composite result.png
You can add as many blank areas as you want by adding more white areas to the mask or by tiling out one region of black and one region of white to create the mask with regular spacing of black and white.
Edited to add this ImageMagick 6 example which splits the input image into 4 pieces, 25% of the original width and 100% of its height, then creates a transparent canvas for each piece the same dimensions of the input image, and locates the pieces at their original offsets on those canvases.
convert input.png -set option:distort:viewport %[w]x%[h] -crop 25x100% \
-virtual-pixel none -distort affine "0,0 %[fx:s.page.x],%[fx:s.page.y]" out%03d.png
The output file names will be numbered starting from zero like "out000.png", etc.
Original message...
Here's a simple command using ImageMagick 7 that can crop an image into any number of pieces, and output all the pieces at their original offsets on transparent backgrounds of the original input dimensions...
magick input.png -crop 100x1# -background none \
-extent "%[fx:s.page.width]x%[fx:s.page.height]-%[fx:s.page.x]-%[fx:s.page.y]" out%03d.png
That "-crop 100x1#" tells it to split the image into a grid 100 pieces wide by 1 piece high. You could just as well specify the crop sizes as percents or numbers of pixels.
Edited again to add:
This following command will split the input image into the individual pieces specified with the "-crop" operator, then shave 5 pixels from every side of each piece, then apply a 5 pixel transparent border to every side of each piece. It will still remember the original locations of the pieces within the input canvas, so the "-distort affine ..." can extend the canvases and place the pieces where they were in the input image.
convert input.png -set option:distort:viewport %[w]x%[h] \
-bordercolor none -background none -virtual-pixel none \
-crop 25x100% -shave 5x5 -border 5x5 \
-distort affine "0,0 %[fx:s.page.x],%[fx:s.page.y]" out%03d.png
To use this command with IM7 you need to change "convert" to "magick".
Given the changes of requirements provided by Kamikaze, here is one way to achieve the split with indent in ImageMagick, assuming I understand correctly.
dim=`convert image.png -format "%wx%h" info:`
convert \( -size $dim xc:none \) null: \( image.png -crop 50x100% -shave 5x5 \) -geometry +5+5 -layers composite result.png
To check, I flatten over a blue background:
convert result-0.png result-1.png -background blue -flatten result.png

Image Magick - Creating animated gif less weight than Photoshop

I could create a less weight animated gif with same variables(colors, dither, etc) using the following function:
convert -delay 4 -loop 0 *.png -coalesce -matte -alpha remove -depth 8 -layers optimizeFrame -colors 128 animated.gif
But there is a Photoshop function called "Includes transparency based on color opacity" that loose a lot of weight, and I can't find an Image Magick equivalent function.
Thanks!
Found it, I have to del -coalesce
convert -delay 4 -loop 0 *.png -matte -alpha remove -depth 8 -layers optimizeFrame -colors 128 animated.gif

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.

how to make a high quality animated image with imagemagick

I want to make an animated gif from those .png image:
I do it with this command:
convert -layers OptimizePlus -delay 25x100 ps1-*.png -loop 0 ps1.gif
It made an animated gif successfully, however, the output has very low quality and smaller than input images:
After some search, I got -quality
convert -layers OptimizePlus -delay 25x100 -quality 99 ps1-*.png -loop 0 ps1.gif
But it seems like imagemagick just ignore the parameter.
The problem is that your source PNGs have an alpha channel which is not supported by GIFs. So you have to remove transparency from your source images first. Since you're dealing with multiple source images, you can't use the -flatten method. With newer ImageMagick versions the following should work:
convert -background white -alpha remove -layers OptimizePlus -delay 25x100 ps1-*.png -loop 0 ps1.gif
If your version of ImageMagick is older than 6.7.5, you can try:
convert -bordercolor white -border 0 -layers OptimizePlus -delay 25x100 ps1-*.png -loop 0 ps1.gif
I got the following result with the latter command:

Resources