Change image color

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
tantrum
Posts: 8
Joined: 2020-01-12T08:19:09-07:00
Authentication code: 1152

Re: Change image color

Post 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

snibgo
Posts: 12938
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Change image color

Post 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.
snibgo's IM pages: im.snibgo.com

tantrum
Posts: 8
Joined: 2020-01-12T08:19:09-07:00
Authentication code: 1152

Re: Change image color

Post 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,

User avatar
fmw42
Posts: 26292
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Change image color

Post 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

tantrum
Posts: 8
Joined: 2020-01-12T08:19:09-07:00
Authentication code: 1152

Re: Change image color

Post by tantrum »

-colorspace sRGB did the trick! thanks so much.

tantrum
Posts: 8
Joined: 2020-01-12T08:19:09-07:00
Authentication code: 1152

Re: Change image color

Post 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

User avatar
fmw42
Posts: 26292
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Change image color

Post 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

Post Reply