Resizing an border image as overlay over another image - imagemagick

Using Imagemagick.
Goal is to overlay an image A with an transparent border image B.
The "main" image A has the following dimensions:
identify examples/1.jpg
examples/1.jpg JPEG 1936x1296 1936x1296+0+0 8-bit DirectClass 1.231MB 0.000u 0:00.000
The transparent border image B:
identify examples/images/rahmen/basic/frame.png
examples/images/rahmen/basic/frame.png PNG 3508x2480 3508x2480+0+0 8-bit DirectClass 60.8KB 0.000u 0:00.000
You can see that the border image is larger than the main image
We use
composite -resize 1936x1296 \
examples/images/rahmen/basic/frame.png \
images/1.jpg \
output.jpg
for generating the overlay.
The resulting image has the border but the border image does not fill up the image border but instead it show up inside the original image.
http://s16.postimage.org/t3vfpv7lx/image.jpg
So how can I resize/downsize the border image to the exact dimensions of the main image
using imagemagick?

Related

Apply cropping mask to the animated gif with a transparent background

I have some animated GIF with a transparent background, e.g:
https://i.imgur.com/5yRvEDc.gif
I also have a mask PNG file, where some pixels are transparent, which I want to apply to the GIF, e.g
https://i.imgur.com/8Ly6Exg.png
I want all transparent pixels on the mask to be transparent on the GIF (for each frame) - just like the image below, but animated AND keep the original GIF transparent pixels.
https://i.imgur.com/08kIjse.png
How it can be achieved with ImageMagick?
Here is one way to do that in ImageMagick 6. First multiply the mask with the alpha channels of the animation. Then put the new alpha channels back with the animation.
I note that your mask, has the circle in the alpha channel of the PNG and the underlying image is totally black. So the important part of your mask image is its alpha channel.
Animation:
Mask:
convert lips.gif -coalesce -alpha extract null: \( lips_mask.png -alpha extract \) -compose multiply -layers composite alpha.gif
convert lips.gif null: alpha.gif -alpha off -compose copy_opacity -layers composite lips_masked.gif
If using ImageMagick 7, change convert to magick.

ImageMagick overlay. Why does it change color?

I overlay the PNG image on background, also in PNG, but at the output I get that the color of the overlay image turned to gray.
How to make sure that the color does not change, but the image just overlays on the background?
convert.exe -composite -gravity center -geometry +0+120 -quality 100 "background.png" "3.png" "4.png"
3.png - Image overlay
4.png - Output image
Background.png - where to put 3.png
all images this

How to convert PNG images (specifically grayscale ones) to Indexed color

I'm trying to convert some small PNG images from 32-bit color mode to indexed color mode.
For color images, I ran the command convert IMGS/FLAME.png INDEXED_IMGS/FLAME.png and it converted fine. For an image that had only grayscale colors, I ran that same command (with the filename changed obviously) but I got a warning:
convert: profile 'icc': 'RGB ': RGB color space not permitted on grayscale PNG 'INDEXED_IMGS/SHADOW.png' # warning/png.c/MagickPNGWarningHandler/1748.
I ran file IMGS/*.pngand got
IMGS/FLAME.png: PNG image data, 16 x 16, 8-bit/color RGBA, non-interlaced
IMGS/SHADOW.png: PNG image data, 8 x 8, 8-bit/color RGBA, non-interlaced
which is expected; both images are in 8-bit RGBA mode (since that's the mode I created them in Photoshop). However, when I run file INDEXED_IMGS/*.png I get
INDEXED_IMGS/FLAME.png: PNG image data, 16 x 16, 4-bit colormap, non-interlaced
INDEXED_IMGS/SHADOW.png: PNG image data, 8 x 8, 8-bit grayscale, non-interlaced
The 4-bit colormap part checks out, but the grayscale part does not.
So my question is: how can I convert a grayscale image to indexed mode? What really gets me is that it starts out in RGBA mode like the color image, but for some reason it converts automatically to grayscale mode. Is there a way to prevent it from doing that?
I should add that I have a bash script that looks like this:
#!/bin/bash
for img in IMGS/*.png; do
file=$(basename $img)
convert $img INDEXED_IMGS/$file
done
so I don't wanna manually distinguish between grayscale and colored images. If there's a way to do so automatically with some command that's fine though.
Here is info about my ImageMagick tool:
Version: ImageMagick 7.0.8-42 Q16 x86_64 2019-04-24 https://imagemagick.org
Copyright: © 1999-2019 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenMP
Delegates (built-in): bzlib freetype heic jng jp2 jpeg lcms ltdl lzma openexr png tiff webp xml zlib
With ImageMagick, for 24-bit color, append the output with PNG8:output
convert input.png PNG8:output.png
PNG grayscale images do not support color profiles, so you get that warning. But the resulting image should be 8-bit palette.
If you have 32-bit color, then this needs more to be done. The color under the alpha channel must be one constant color and one not used elsewhere in the image. Find such a color after converting to 256 colors and set the color under the transparency to that color. For example if you have no opaque black in your image after converting to 256 colors, then set the alpha base color to black.
convert image.png +dither -colors 256 -background black -alpha background PNG8:output.png
You can get a list of unique colors from the image as follows:
convert image.png +dither -colors 256 -unique-colors txt:
Here is an example:
Make it 32-bit transparent:
convert rose.png -fuzz 20% -transparent red rose_trans32.png
identify -verbose rose_trans32.png
...
Colorspace: sRGB
Type: TrueColorAlpha
...
Convert to palette alpha:
convert rose_trans32.png -alpha off +dither -colors 256 -unique-colors txt:
List shows no black
convert rose_trans32.png +dither -colors 256 -background black -alpha background PNG8:rose_trans8.png
Or if you already know that the 32-bit version has not black, then just:
convert rose_trans32.png -background black -alpha background PNG8:rose_trans8.png
identify -verbose rose_trans8.png
...
Colorspace: sRGB
Type: PaletteAlpha
...
If you do this adding -colorspace gray, ImageMagick will still report as type grayscalealpha, since it recognizes it as a single channel image with transparency. But using EXIFTOOL, it will report 9 ColorType: 3, which is 3 = RGB Palette
NOTE: For ImageMagick 7, change convert to magick.

Wrap a gif with a png

There are two image, one is gif, another is png. Now I need to wrap the gif image with png image. I use ImageMagick and I think it may work for me.
convert src.png input.gif -gravity Center -composite des.gif;
As you see, the output is unexpected while there is a black block.
And the des.gif is not animated.
left is des.gif the right is input.gif
EDIT
Here is my test files. https://drive.google.com/drive/folders/0B-64AliLi9OnWVQyLWU3TElHNHM?usp=sharing
You have 77 frames. But the first frame is much smaller than all the rest. From Imagemagick (6.9.9.13 Q16 Mac OSX Sierra), I can see this from
identify input.gif
input.gif[0] GIF 216x384 216x384+0+0 8-bit sRGB 256c 3.20786MiB 0.010u 0:00.009
input.gif[1] GIF 1080x1920 216x384+0+0 8-bit sRGB 64c 3.20786MiB 0.010u 0:00.009
...
input.gif[76] GIF 1080x1920 216x384+0+0 8-bit sRGB 64c 3.20786MiB 0.000u 0:00.000
Also all frames but the first are near solid green. Only the first has a circle in it.
Nevertheless, if you fix your input.gif, you can composite them so all the gif frames animate over the background png using the following command:
convert image.png null: \( input.gif -coalesce \) -gravity Center -layers composite des.gif
Note that after the coalesce all frames become 216x384. That makes the overlay frames way too small. This will make all frames larger so that they fit the background. But still all will be green exact for the first.
convert image.png null: \( input.gif -coalesce -resize 1080x1920! \) -gravity Center -layers composite des.gif
See http://www.imagemagick.org/Usage/anim_mods/#background
If using Imagemagick 7, then change convert to magick.

Flatten PNG image with ImageMagick

Using shell commands, I can perform the following on a PNG file that has transparency:
convert image.png -background Black -flatten image.png
And the resulting image now has a black background. How do I do this with the Magick++ API? I have an Image object that I'm using already for some other manipulation:
Image img(filename);
img.resize(Magick::Geometry("x48"));
img.unsharpmask(5.0, 0.5, 50.0, 50.0);
img.gamma(0.5);
...
Before I do the resizing, I need to take care of the image transparency and make it black instead.
Thanks.
The -flatten option can be found in STL.h and is called flattenImages. This method requires a container of images. Below is an example of how you can use the method.
Image img(filename);
Geometry size(img.columns(), img.rows());
Color color(0,0,0);
Image black(size, color);
std::list<Image> images;
images.push_back(black);
images.push_back(img);
Image flattenedImage;
flattenImages(&flattenedImage, images.begin(), images.end());
flattenedImage.resize(Geometry("x48"));

Resources