Stop ImageMagick from pre-multiplying during composite - imagemagick

ImageMagick is premultiplying transparent pixels. This causes a gray outline to appear during subsequent transformations.
For example:
$ convert -size 1085x558 xc:"rgba(0,0,0,0)" PNG32:temp.png
$ composite -gravity center samples/logo_white.png temp.png PNG32:temp.png
Here are the source and resulting images.
Here is a video showing that the temp.png image has had its transparent pixels turned from white to black.
Is there a way to force ImageMagick to leave fully transparent pixels alone rather than changing them to black?

Related

How to remove alpha channeled margins from an image using ImageMagick

I have an image that all four margins are transparent alpha channels and I want to crop all margins and only keep the non-alpha channel parts. Is there any way to do it with ImageMagick? Take this image as an example but consider its transparent margins!
You can do that in Imagemagick using -trim to automatically remove the constant transparent regions.
convert img.png -trim +repage result.png

How to remove specific white space from image using ImageMagick

Currently, I have thousands of pictures where I want to get rid of white space around the object, but not inside the object. However, the white space is not constant. See pictures below.
Picture 1 :
Picture 2 :
Picture 3 :
Options like this don't work as they delete all white space in the images
convert image.png -fuzz 10% -transparent white output.png
Any suggestions?
What do you mean by getting rid of the white space? If you mean make it transparent, then in ImageMagick 6, you can do a whole folder of images using the mogrify command as follows:
The process involves padding the image with 1 pixel border of white to ensure white is all around. Then doing a fuzzy flood fill. Then remove the extra one pixel all around. I have assumed that the background color is nearly white. Change the fuzz value as desired.
Create a new directory to hold the output (or backup your input directory)
Change directory to the one holding your input images
mogrify -path path_to/outdirectory -format png -bordercolor white -border 1 -fuzz 20% -fill none -draw "matte 0,0 floodfill" -shave 1x1 *.png
Here are the resulting images.
Of course you will have to download them to see that they are transparent.
Note that the images that I downloaded were JPG. So if your images are jpg and not png as in your command, then change *.png to *.jpg. Or just use * to change all formats.
See
https://imagemagick.org/Usage/basics/#mogrify
https://imagemagick.org/Usage/draw/#matte
If using ImageMagick 7, change matte to alpha and change mogrify to magick mogrify.

ImageMagick treating JPG and GIF differently in -alpha shape

I just encountered unexpected behavior in ImageMagick, which I'm hoping someone can explain to me.
Version numbers
$ convert --version
Version: ImageMagick 6.7.7-10 2013-02-25 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
Features: OpenMP
Running on Linux Mint 15 Olivia (based on Ubuntu 13.04 "Raring Ringtail").
Executive summary
Running the same operation with two images that should be quite similar, except that one is a GIF while the other is a JPEG, the resulting output is entirely different. The GIF comes out black, the JPEG comes out white.
To reproduce:
Go to http://karenswhimsy.com/public-domain-images/animal-silhouettes/animal-silhouettes-1.shtm and download the elephant silhouette as elephant.jpg
Download http://www.arthursclipart.org/silhouettes/animals/DUCK1.gif as DUCK1.gif
convert elephant.jpg -negate -alpha shape output-elephant.png
convert DUCK1.gif -negate -alpha shape output-duck.png
Compare the output-elephant.png and output-duck.png images
Can anyone explain why these two output images are different? Why the GIF duck ends up being black after conversion, while the JPEG elephant becomes white after conversion?
Long-winded explanation
The situation is this: I want to take some black-and-white images, turn the background transparent, and turn the foreground different colors. For example, starting with the elephant silhouette at http://karenswhimsy.com/public-domain-images/animal-silhouettes/images/animal-silhouettes-1.jpg (I saved the image as elephant.jpg), I want to produce a .png with a transparent background and an elephant that's green, red, yellow, or whatever color I want.
The command I'm using to do this is:
convert elephant.jpg -negate -alpha shape +level-colors ,green green-elephant.png
This does exactly what I want. First it inverts the image so that the background is black and the elephant is white, because -alpha shape expects an alpha mask where black = fully transparent and white = fully opaque. Then -alpha shape does its magic and produces a white elephant against a transparent background. Then +level-colors ,green kicks in, transforming black-and-white gradients into the two colors specified as parameters to +level-colors; here, the first color is omitted so it would remain black, and the second color is what white turns into. This produces a green elephant with smooth borders -- exactly the result I want -- and I'm quite happy with it.
Next, I tried running the same command against one of the images from http://www.arthursclipart.org/silhouettes/animals.htm (I used DUCK1.gif). The input source is the same -- a black silhouette against a white background -- so I expected the same result, a green duck against a transparent background. But it didn't work. A little research showed me that -alpha shape was behaving differently. Where with the JPEG elephant it was producing a white elephant against a transparent background, with the GIF duck the same command was producing a black duck against a transparent background. In other words, -alpha shape was inverting the result with a GIF image source, but not with a JPEG image source. So to color the animal properly, I needed to rewrite the +level-colors parameters to put "green" before the comma instead of after.
Tweaking my script is no problem at all, but I'd love to understand why this is happening, and so far I'm clueless. Can anyone explain to me why ImageMagick is treating GIFs and JPEGs so differently in the -alpha shape operation?
OK, I think here is the story. With GIF, a background color might be explicitly defined, and I think that is the case with DUCK1.GIF. Not so with JPEG; here the background is, I think, assumed to be "white".
Whatever the case is, converting the DUCK1.GIF to DUCK1.JPG results in a similar image to the elephant. Moreover, you can make sure that the same background color is used with the apropriate Imagemagick option. The two commands below produce similar output:
convert DUCK1.jpg -background Black -negate -alpha shape output-duck.png
convert elephant.jpg -background Black -negate -alpha shape output-elephant.png

Imagemagick composite blur results in black area

I am composing an image from two input images - one with text and one background image.
When using the composite -blur option I get black areas to the right and to the bottom of the overlaying text. Example here:
This is my imagemagick command:
composite -blur 400 -geometry +16+193 example.png background.jpg result.png
I have tried the repage and trim commands to avoid the black areas but that did not work.
Does anyone have a good tip for me?

imagemagick - animate, with a static black background?

Just a quickie.
I have a series of images min*.png that I want to animate into a gif.
They are each fully transparent, except for some white dots on the area I want filled in for that frame.
Is there some way to create an animation from these such that the background is black (so that the whtie dots show up?)
I am interested in both:
black background, and paste each successive image on top of the previous ones (so frame i is the black background plus all of the dots up to image i)
each frame consists of just (image i on a black background)
I think for 1. I need to use -dispose none and for 2 I use -dispose background or -dispose previous, but various attempts at actually setting the background to black have failed (I have spent a lot of time reading this imagemagick page but am still learning).
e.g.
convert -background black -dispose background min*.png out.gif
various attempts with -background and -dispose have invariably produced a gif of my min*.png with a transparent background, not a black one. I think I'm close, but not sure.
This may be useful for the black background problem: starting from ImageMagick 6.7.5 you can remove transparency and replace it with a static color; you can read more about this command here
Hope this helps, unfortunately I have an older version of Imagemagick, so i can't try it myself
Example from ImageMagick documentation:
convert moon.png -background tan -alpha remove alpha_remove.png
The color "tan" replaces the transparent areas of the picture
Comment from mathematical.coffee
Using the above answer, I was able to generate the animations I wanted.
1: successive buildup of dots, all on a black background. Turned out to be as simple as creating a black background picture to put at the start of the animation, and using -coalesce:
# where bg.png is a black png of the appropriate size:
convert bg.png min*.png -coalesce out.gif
# in the below the first line creates the black background
# image, same size as my first min00.png image, for me:
convert min00.png -alpha Opaque +level-colors black \
min*.png -coalesce out.gif
2. use the method mentioned above:
convert min000*.png -background black -alpha remove out.gif
In both I was using imagemagick 6.7.something.

Resources