Problem changing solid color to transparent color

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
jcompton

Problem changing solid color to transparent color

Post by jcompton »

I can't quite seem to get the right syntax to change an existing color with FF transparency to non-FF transparency.

I wish to change a bunch of near-black shadows to be 0x00000080--all-black, but only 50% transparency. The source files are PNGs with completely transparent backgrounds.

Code: Select all

mogrify -path outdir -matte -fill rgba(0,0,0,0.5) -opaque rgb(31,22,13) *.png
This gets me half of what I'm looking for: the 31,22,13 near-black is changed to 0,0,0 , but it is still fully FF/255 opaque.

What's the secret sauce I'm missing?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Problem changing solid color to transparent color

Post by fmw42 »

jcompton wrote:I can't quite seem to get the right syntax to change an existing color with FF transparency to non-FF transparency.

I wish to change a bunch of near-black shadows to be 0x00000080--all-black, but only 50% transparency. The source files are PNGs with completely transparent backgrounds.

Code: Select all

mogrify -path outdir -matte -fill rgba(0,0,0,0.5) -opaque rgb(31,22,13) *.png
This gets me half of what I'm looking for: the 31,22,13 near-black is changed to 0,0,0 , but it is still fully FF/255 opaque.

What's the secret sauce I'm missing?

set the opacity percent =(100-transparency) percent using

-alpha on -channel o -evaluate set <opacity percent>

e.g

-alpha on -channel o -evaluate set 60% sets the transparency to 40%

see

http://www.imagemagick.org/Usage/basics/#alpha
http://www.fmwconcepts.com/imagemagick/ ... #ave_alpha
http://www.imagemagick.org/script/comma ... p#evaluate
http://www.imagemagick.org/Usage/transform/#evaluate
jcompton

Re: Problem changing solid color to transparent color

Post by jcompton »

Thanks, but I'm still not getting the right results.

Code: Select all

mogrify -path outdir -alpha on -channel o -evaluate set 50% -fill rgba(0,0,0,0.5) -opaque rgb(31,22,13) *.png
turns the entire image 50% transparent. I only want the changed black pixels to be 50% transparent.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Problem changing solid color to transparent color

Post by fmw42 »

jcompton wrote:Thanks, but I'm still not getting the right results.

Code: Select all

mogrify -path outdir -alpha on -channel o -evaluate set 50% -fill rgba(0,0,0,0.5) -opaque rgb(31,22,13) *.png
turns the entire image 50% transparent. I only want the changed black pixels to be 50% transparent.

Sorry I misunderstood. Try

mogrify -path outdir -matte -fill "rgba(0,0,0,0.5)" -opaque "rgba(31,22,13,1)" *.png
jcompton

Re: Problem changing solid color to transparent color

Post by jcompton »

Afraid not. That only generates the 0x000000FF pixels I was getting with my original attempts.

I'll paste in my exact command just to be super-certain we're not getting any wires crossed here:

Code: Select all

C:\downloads\tbh_reiner>mogrify -path shadowtest.image -matte -fill "rgba(0,0,0,0.5)" -opaque "rgba(31,22,13,1.0)" reiner.image\*.png
(I tried it initially with "1" as you recommended instead of "1.0", no difference.)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Problem changing solid color to transparent color

Post by fmw42 »

jcompton wrote:Afraid not. That only generates the 0x000000FF pixels I was getting with my original attempts.

I'll paste in my exact command just to be super-certain we're not getting any wires crossed here:

Code: Select all

C:\downloads\tbh_reiner>mogrify -path shadowtest.image -matte -fill "rgba(0,0,0,0.5)" -opaque "rgba(31,22,13,1.0)" reiner.image\*.png
(I tried it initially with "1" as you recommended instead of "1.0", no difference.)

This seems to work with convert. Need to set -channel rgba

convert -size 256x256 gradient: -alpha on -channel rgba -fill "rgba(255,255,255,0.5)" -opaque "rgba(20,20,20,1)" tmp.png
jcompton

Re: Problem changing solid color to transparent color

Post by jcompton »

-channel rgba was the missing piece to the puzzle, thanks!
jcompton

Re: Problem changing solid color to transparent color

Post by jcompton »

This is working well for the original purpose, but it turns out that it would also be useful to be able to map any (0,0,0,x) (any black, regardless of transparency level, including 0.0 and 1.0) to be (0,0,0,0.5) . Any ideas how I can make that happen? Maybe I'm misunderstanding things but the -opaque and -transparent operators seem to be getting in my way and I can't even work out how to do it in two passes.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Problem changing solid color to transparent color

Post by fmw42 »

jcompton wrote:This is working well for the original purpose, but it turns out that it would also be useful to be able to map any (0,0,0,x) (any black, regardless of transparency level, including 0.0 and 1.0) to be (0,0,0,0.5) . Any ideas how I can make that happen? Maybe I'm misunderstanding things but the -opaque and -transparent operators seem to be getting in my way and I can't even work out how to do it in two passes.

This is at least one way to do it, but it is slow:

create test gradient with gradient alpha channel:
convert -size 256x256 gradient: -alpha copy tmp.png

change any pixel with graylevel r=g=b=20 (range 0-255) irrespective of alpha value to r=g=b 200 with mid value (127) alpha. NOTE this appears to be an inconsistency in how alpha is specified in -fx as rgba values should be specified with alpha in range 0 to 1 whether rgb are in range 0-255 or 0-100%. I tried alpha of 0, 1 and 0.5 and it objected when I used 0.5

convert tmp.png -alpha on -channel rgba -fx 'u.r==20/255&&u.g==20/255&&u.b==20/255?rgba(200,200,200,127):u' tmp1.png

Perhaps Anthony has a better way and can explain this potential inconsistency.
Post Reply