Page 2 of 2

Re: Change image color

Posted: 2020-01-12T08:33:56-07:00
by tantrum
Hi I'm having trouble with replace color in a png. This works for a jpg.


Version: ImageMagick 7.0.9-14 Q16 x86_64 2020-01-06 https://imagemagick.org
Copyright: © 1999-2020 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenMP(3.1)
Delegates (built-in): bzlib freetype heic jng jp2 jpeg lcms ltdl lzma openexr png tiff webp xml zlib
Running OS X

>> I create my png-- black on a transparent background. this works fine.

magick -size 1000x500 -background transparent -fill black -font Arial -density 300 -units pixelsperinch caption:"TEST" out.png

>> trying to change the black to yellow and keep transparent background, but this doesn't change the image at all-- still black on transparent background output:

magick out.png -fill yellow +opaque black newyellow.png

Re: Change image color

Posted: 2020-01-12T09:11:45-07:00
by snibgo
See https://www.imagemagick.org/script/comm ... php#opaque . "+opaque black" will change pixels that are not black.

Instead, use "-opaque black" to change pixels that are black.

Re: Change image color

Posted: 2020-01-12T11:20:01-07:00
by tantrum
Thanks,

magick out.png -fill yellow -opaque black newyellow.png
>> makes no change.

I believe it's something with transparency on the image. The reason I say that if i flatten out out.png to a jpg and then run:

magick out.jpg -fill yellow -opaque black newyellow.png things work.

thanks,

Re: Change image color

Posted: 2020-01-12T12:38:25-07:00
by fmw42
Your image is bilevel with alpha. IM 7 looks at that as one channel. To colorize it, you must convert it to RGB. So try either of the following by adding -colorspace sRGB

Code: Select all

magick out.png -colorspace sRGB -fill yellow -opaque black newyellow.png

or

magick out.png -colorspace sRGB -fill "rgba(255,255,0,1)" -opaque "rgba(0,0,0,1)" newyellow.png

Re: Change image color

Posted: 2020-01-12T12:47:20-07:00
by tantrum
-colorspace sRGB did the trick! thanks so much.

Re: Change image color

Posted: 2020-02-02T22:44:03-07:00
by tantrum
As I was playing around with this with images with various colors vs just an image with black, I have a slight problem.

magick infile2.png -colorspace sRGB -fill pink -opaque black outfile2.png

The problem is it keeps the non black colors correct but on the black, it convert most of it to pink, BUT the issue is the border is still black around the image "to". I can get rid of it playing with a fuzz %, but when I do that, the other colors in the image then change to black as well.

files:
https://i.ibb.co/FhC66Yg/infile2.png
https://i.ibb.co/L63vDN4/outfile.png
https://ibb.co/GCNSLkW
https://ibb.co/5ML43d7

I did discover one thing upon further testing. I put a black box in via photoshop (set infile3). Ran the same command and that converted fine, with no border. I create the word "to" with Caption:"to", so I suspect magick is not setting all pixels to black when it creates that image using caption? it's like it puts a border around the pure black or the letters.

Version: ImageMagick 7.0.9-14 Q16 x86_64 2020-01-06 https://imagemagick.org
Copyright: © 1999-2020 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenMP(3.1)
Delegates (built-in): bzlib freetype heic jng jp2 jpeg lcms ltdl lzma openexr png tiff webp xml zlib

Re: Change image color

Posted: 2020-02-02T23:44:43-07:00
by fmw42
The issue is that you have an alpha channel that has antialiasing. So pixels around the letters are not fully black or fully white. Thus you need to use -fuzz. Alternately, make the alpha channel binary by thresholding. Then if needed antialias again.

Try this:

Code: Select all

magick infile2.png -channel a -threshold 50% +channel -fill pink -opaque black outfile2.png