Page 1 of 1

Reducing Alpha during multiply

Posted: 2018-11-08T22:06:54-07:00
by abeering
Hey all,

I got a bit of a problem. We have some software that programmatically combines several pngs into a jpg, using a combination of 'over' and 'multiply' conversions.

Code: Select all

convert ( L0_Leg_Metal_Arc_Chrome.png Sofa_C1_L1_001.png -compose over -composite ) ( -clone 0 C1_L2_001.png -compose over -composite ) -delete 0 ( -clone 0 C1_L3_001.png -compose over -composite ) -delete 0 ( -clone 0 C1_L5_017.png -compose multiply -composite ) -delete 0 ( -clone 0 C1_L6_017.png -compose multiply -composite ) -compose over -composite -background White -layers Flatten foo.jpg
I know there is probably a less verbose way to do this, but this syntax/structure really helps when constructing our imagemagick commands.

SO the problem is, when we multiply, as with C1_L5_017.png and C1_L6_017.png, I would like to reduce the alpha (opacity, in case i am misusing alpha) of those two layers. So when I multiply, I want to multiply against the previous composite this new layer at varying opacities. Specifically, 20 and 50 percent, respectively.

Any help would be appreciated.

Re: Reducing Alpha during multiply

Posted: 2018-11-09T00:19:37-07:00
by fmw42
Add -alpha on -channel a -evaluate set 20% +channel after the image you want to reduce its opacity

Re: Reducing Alpha during multiply

Posted: 2018-11-09T13:00:46-07:00
by abeering
fmw42 wrote: 2018-11-09T00:19:37-07:00 Add -alpha on -evaluate set 20% +channel after the image you want to reduce its opacity
Hm, I'm having a bit of trouble making this work. So first I just tried to apply it to one image, to see what I was working with.

Code: Select all

convert C1_L5_017.png -alpha on -evaluate set 50% +channel foo.png
But the resulting image is just .. fully empty / transparent? And has an interesting `file` result: foo.png: PNG image data, 4000 x 4000, 16-bit gray+alpha, non-interlaced

I tried doing it with my parenthetical command, and I actually get an error.

Code: Select all

convert ( C1_L0_Leg_Metal_Arc_Chrome.png C1_L1_001.png -compose over -composite ) ( -clone 0 C1_L2_001.png -compose over -composite ) -delete 0 ( -clone 0 C1_L3_001.png -compose over -composite ) -delete 0 ( -clone 0 Sofa_C1_L5_017.png -alpha on -evaluate set 20% +channel -compose multiply -composite ) -delete 0 ( -clone 0 C1_L6_017.png -compose multiply -composite ) -compose over -composite -background White -layers Flatten foo.jpg

Code: Select all

convert: invalid argument for option '-evaluate': +channel @ error/convert.c/ConvertImageCommand/1515.
Sorry, I'm really a scrub at imagemagick.. I'm reading through docs now but am still *super* confused.

Re: Reducing Alpha during multiply

Posted: 2018-11-09T13:06:50-07:00
by fmw42
What is your Imagemagick version, date and platform/OS?

The command should be

Code: Select all

convert image.png -alpha on -channel a -evaluate set 50% +channel result.png
Sorry, I forgot to add the -channel a above and have edited it to correct it.

Image

Code: Select all

convert lena.png -alpha on -channel alpha -evaluate set 50% +channel lena_50pct.png
Image

Re: Reducing Alpha during multiply

Posted: 2018-11-09T13:12:59-07:00
by abeering
fmw42 wrote: 2018-11-09T13:06:50-07:00 What is your Imagemagick version, date and platform/OS?

The command should be

Code: Select all

convert image.png -alpha on -channel a -evaluate set 50% +channel result.png
Sorry, I forgot to add the -channel a above and have edited it to correct it.

Image

Code: Select all

convert lena.png -alpha on -channel alpha -evaluate set 50% +channel lena_50pct.png
Image

That command worked for me. Here is my imagemagick version:

Code: Select all

Version: ImageMagick 7.0.5-0 Q16 x86_64 2017-02-21 http://www.imagemagick.org
But I'm still having hard time getting this to fit into my computed convert command, I'm sure it's just a matter of an extra parentheses somewhere or just an order problem. Any ideas on this?

Code: Select all

convert ( C1_L0_Leg_Metal_Arc_Chrome.png C1_L1_001.png -compose over -composite ) 
( -clone 0 C1_L2_001.png -compose over -composite ) 
-delete 0 ( -clone 0 C1_L3_001.png -compose over -composite ) 
-delete 0 ( -clone 0 ( -alpha on -channel a -evaluate set 50% C1_L5_017.png ) -compose multiply -composite ) 
-delete 0 ( -clone 0 C1_L6_017.png -compose multiply -composite ) 
-compose over -composite -background White -layers Flatten foo.jpg
This gets me convert: invalid argument for option '-evaluate': Sofa_C1_L5_017.png @ error/convert.c/ConvertImageCommand/1515.

EDIT: +channel was missing in code block, actually looks like

... ( -alpha on -channel a -evaluate set 50% +channel Sofa_C1_L5_017.png ) ...

convert: invalid argument for option '-evaluate': +channel @ error/convert.c/ConvertImageCommand/1515.

Re: Reducing Alpha during multiply

Posted: 2018-11-09T13:16:24-07:00
by fmw42
-delete 0 ( -clone 0 ( -alpha on -channel a -evaluate set 50% C1_L5_017.png ) -compose multiply -composite )
To which image do you want to apply the reduced alpha -- the -clone 0 or the C1_L5_017.png

Re: Reducing Alpha during multiply

Posted: 2018-11-09T13:17:24-07:00
by abeering
The latter, the C1_L5_017.png.

To be clearer, I want to multiply layer a with layer b, and before/during that I want to reduce opacity of layer b to 50%.

Re: Reducing Alpha during multiply

Posted: 2018-11-09T13:22:26-07:00
by fmw42
try putting your processing after the reading the image you want it to be applied.

Code: Select all

-delete 0 ( -clone 0 ( C1_L5_017.png -alpha on -channel a -evaluate set 50% ) -compose multiply -composite )

Re: Reducing Alpha during multiply

Posted: 2018-11-09T13:29:25-07:00
by abeering
fmw42 wrote: 2018-11-09T13:22:26-07:00 try putting your processing after the reading the image you want it to be applied.

Code: Select all

-delete 0 ( -clone 0 ( C1_L5_017.png -alpha on -channel a -evaluate set 50% ) -compose multiply -composite )
Yeah, I preemptively tried that. Still getting an argument error for set.

Your example (with no +channel):

Code: Select all

-delete 0 ( -clone 0 ( C1_L5_017.png -alpha on -channel a -evaluate set 50% ) -compose multiply -composite )
convert: invalid argument for option '-evaluate': ) @ error/convert.c/ConvertImageCommand/1515.
with +channel:

Code: Select all

-delete 0 ( -clone 0 ( C1_L5_017.png -alpha on -channel a -evaluate set 50% +channel ) -compose multiply -composite )
convert: invalid argument for option '-evaluate': +channel @ error/convert.c/ConvertImageCommand/1515.

Re: Reducing Alpha during multiply

Posted: 2018-11-09T14:09:09-07:00
by abeering
fmw42 wrote: 2018-11-09T13:16:24-07:00
-delete 0 ( -clone 0 ( -alpha on -channel a -evaluate set 50% C1_L5_017.png ) -compose multiply -composite )
To which image do you want to apply the reduced alpha -- the -clone 0 or the C1_L5_017.png
First off I REALLY appreciate the help you've provided so far. You're saving my life. I have one other question.

Here is an example of me reducing the alpha to 20% using the methodology laid out here. The source image has a fully transparent background, but the new image is attaining a sort of grey shade.

Image

Any idea what's happening here?

Re: Reducing Alpha during multiply

Posted: 2018-11-09T14:21:46-07:00
by fmw42
If your image already has transparency, then -evaluate set will make it all that value. So you need to reduce the existing opacity/transparency by mutiplication. try for 50% reduction, multiply by 0.5

Code: Select all

-delete 0 ( -clone 0 ( C1_L5_017.png -alpha on -channel a -evaluate multiply 0.5 +channel ) -compose multiply -composite )

Re: Reducing Alpha during multiply

Posted: 2018-11-09T15:01:49-07:00
by abeering
fmw42 wrote: 2018-11-09T14:21:46-07:00 If your image already has transparency, then -evaluate set will make it all that value. So you need to reduce the existing opacity/transparency by mutiplication. try for 50% reduction, multiply by 0.5

Code: Select all

-delete 0 ( -clone 0 ( C1_L5_017.png -alpha on -channel a -evaluate multiply 0.5 +channel ) -compose multiply -composite )
Thanks so much for all your help. You're a hero!