Page 1 of 2

find opacity of pixels alpha layer

Posted: 2017-03-16T05:04:32-07:00
by bossrunner1
Hi everybody.
I've been trying to work this out for days.
I have an image and with the command line i want to find and delete all pixels that are below a given opacity level..
I.e. delete all pixels below 30% opacity.
How can this be achieved please as i cannot work this one out.
Regards and thanks
Matt

Re: find opacity of pixels alpha layer

Posted: 2017-03-16T05:08:54-07:00
by dlemstra
Can you clarify what you mean by deleting the pixel? What should happen with it? Should it become transparent?

Re: find opacity of pixels alpha layer

Posted: 2017-03-16T05:11:37-07:00
by bossrunner1
thanks for the reply.
Yes, the pixel should be transparent.
Matt

Re: find opacity of pixels alpha layer

Posted: 2017-03-16T07:19:39-07:00
by snibgo
"-channel A -black-threshold 30% +channel" should do it.

Re: find opacity of pixels alpha layer

Posted: 2017-03-16T07:39:41-07:00
by bossrunner1
thankyou...
"convert in.png -channel A -black-threshold 30% +channel out.png"
that command does something but it does no make the pixels transparent and instead just shows the original image that was faded.
i have a faded image and what i want to be able to do is make 100% transparent pixels that are say only 30% opaque or less.
Hope you can help out
Matt

Re: find opacity of pixels alpha layer

Posted: 2017-03-16T07:53:08-07:00
by snibgo
What version of IM are you using? In some versions, IM was confused about alpha (because it used to store transparency, not opacity) so thresholds worked the wrong way round. For those cases, we can "-alpha extract", and "-black-threshold" that, then CopyOpacity the alpha channel back.

What shell do you use? If Windows BAT, the % needs doubling.

Can you show an example input? You can upload to somewhere like dropbox.com and paste the URL here.

Re: find opacity of pixels alpha layer

Posted: 2017-03-16T08:24:30-07:00
by bossrunner1
Thanks a lot.
To answer your questions.
Im using the command line on ubuntu.
Here is the image. https://www.dropbox.com/s/7efapn3t3vp5t ... e.png?dl=0
Basically the goal is 2 steps.
Step 1 make transparent the pixels that are 30% transparent or less.
Step 2 put a color for example red under every pixel (not the fully transparent ones).
The end result is an image with no semi transparent pixels at all and those that were semi transparent now have a red background colour.
Appreciate your help
Matt

Re: find opacity of pixels alpha layer

Posted: 2017-03-16T08:40:19-07:00
by GeeMack
bossrunner1 wrote: 2017-03-16T07:39:41-07:00i have a faded image and what i want to be able to do is make 100% transparent pixels that are say only 30% opaque or less.
Using IM 6.9.7 on Windows 10 64 I can get that result by running this command...

Code: Select all

convert input.png -channel A -fx "p*(p>0.3?1:0)" result.png
That uses "-channel A" to work on just the alpha channel. It checks each pixel to see if it's alpha value is greater than 30%. If it is, it multiplies the pixel's alpha value by 1 which leaves the transparency as it was. If the pixel's alpha value is 30% or less it multiplies the value by 0, which results in changing that to a fully transparent pixel.

Re: find opacity of pixels alpha layer

Posted: 2017-03-16T08:55:14-07:00
by bossrunner1
Thank worked fantastically and i appreciate you explaining how you did it... I understand for next time.

How would i put the red under the semi transparent part please?

Re: find opacity of pixels alpha layer

Posted: 2017-03-16T09:00:27-07:00
by snibgo
GeeMack got there before I did. GeeMack's solution will work, but "-fx" is slow. I find it too slow to be useful on large images.

The fx might be faster written as:

Code: Select all

-fx "p>0.3:p:0"
... so there is no multiplication.


You didn't say what version of IM you use. I assume v6. If v7, change "convert" to "magick".

The following turns all alpha values that are below 30% to zero. Thus, pixels that are mostly transparent become entirely transparent. This is Step (1) only.

Windows BAT syntax. For bash, escape each parentheses to \( and \), and change line-endings from ^ to \ , and change each doubled %% to a single % .

Code: Select all

convert ^
  test-image.png ^
  ( +clone -alpha extract -black-threshold 30%% ) ^
  -compose CopyOpacity -composite ^
  ti_30.png
The following does your steps (1) and (2) together. It works by making a clone, and flattening it entirely against red. Then, as before, it applies a threshold to the alpha.

Code: Select all

convert ^
  test-image.png ^
  ( -clone 0 -background Red -layers flatten ) ^
  ( -clone 0 -alpha extract -black-threshold 30%% ) ^
  -delete 0 ^
  -compose CopyOpacity -composite ^
  ti_30_r.png
I assume you don't want to write the output of step (1) to a file. If you do, you could run both commands, or a single slightly more complex command.

Re: find opacity of pixels alpha layer

Posted: 2017-03-16T09:12:39-07:00
by bossrunner1
Thanks to both of you... Fantastic work!!

I tried this and am on IM6.x and it works 99% as needed.
The red shows up but is semi transparent. What i need is the red 100% full pixel color not transparent with the original semi transparent pixel on top of it.
Hope this makes sense.

Re: find opacity of pixels alpha layer

Posted: 2017-03-16T09:21:58-07:00
by snibgo
Oops, sorry. In my second command, change "-black-threshold" to "-threshold".

Re: find opacity of pixels alpha layer

Posted: 2017-03-16T09:26:36-07:00
by GeeMack
bossrunner1 wrote: 2017-03-16T09:12:39-07:00The red shows up but is semi transparent. What i need is the red 100% full pixel color not transparent with the original semi transparent pixel on top of it.
Adding to my example above and including the ideas from snibgo's examples, you might try something like this...

Code: Select all

convert input.png -channel A -fx "p>0.3?p:0" \
   \( -clone 0 -fill red -colorize 100 -clone 0 -compose copyopacity -composite \) \
   +swap -compose over -composite result2.png
That works for me with IM 6.7.7 in a bash shell, or with IM 6.9.7 in Windows with a little adjustment to the syntax.

Re: find opacity of pixels alpha layer

Posted: 2017-03-16T09:32:43-07:00
by bossrunner1
Thanks geemack.
This doesn't work here - red is still transparent.
I have IM 6.697
Thankyou

Re: find opacity of pixels alpha layer

Posted: 2017-03-16T10:02:23-07:00
by fmw42
snibgo wrote:Oops, sorry. In my second command, change "-black-threshold" to "-threshold".
try snibgo's modification written in unix as

Code: Select all

convert \
  test-image.png \
  \( -clone 0 -background Red -layers flatten \) \
  \( -clone 0 -alpha extract -threshold 30% \) \
  -delete 0 \
  -compose CopyOpacity -composite \
  ti_30_r.png
or

Code: Select all

convert \
  test-image.png \
  \( -clone 0 -alpha off -fill red -colorize 100 \) \
  \( -clone 0 -alpha extract -threshold 30% \) \
  -delete 0 \
  -compose CopyOpacity -composite \
  ti_30_r.png
If that is not what you want, the please explain more clearly what should happen to the pixels that have opacity > 30%. Should one see those pixels or should they be opaque red or should they be partially transparent red?

Perhaps an example of your input and desired output results would help.