find opacity of pixels alpha layer

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?".
bossrunner1
Posts: 8
Joined: 2017-03-16T05:00:27-07:00
Authentication code: 1151

find opacity of pixels alpha layer

Post 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
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: find opacity of pixels alpha layer

Post by dlemstra »

Can you clarify what you mean by deleting the pixel? What should happen with it? Should it become transparent?
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
bossrunner1
Posts: 8
Joined: 2017-03-16T05:00:27-07:00
Authentication code: 1151

Re: find opacity of pixels alpha layer

Post by bossrunner1 »

thanks for the reply.
Yes, the pixel should be transparent.
Matt
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: find opacity of pixels alpha layer

Post by snibgo »

"-channel A -black-threshold 30% +channel" should do it.
snibgo's IM pages: im.snibgo.com
bossrunner1
Posts: 8
Joined: 2017-03-16T05:00:27-07:00
Authentication code: 1151

Re: find opacity of pixels alpha layer

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: find opacity of pixels alpha layer

Post 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.
snibgo's IM pages: im.snibgo.com
bossrunner1
Posts: 8
Joined: 2017-03-16T05:00:27-07:00
Authentication code: 1151

Re: find opacity of pixels alpha layer

Post 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
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: find opacity of pixels alpha layer

Post 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.
bossrunner1
Posts: 8
Joined: 2017-03-16T05:00:27-07:00
Authentication code: 1151

Re: find opacity of pixels alpha layer

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: find opacity of pixels alpha layer

Post 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.
snibgo's IM pages: im.snibgo.com
bossrunner1
Posts: 8
Joined: 2017-03-16T05:00:27-07:00
Authentication code: 1151

Re: find opacity of pixels alpha layer

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: find opacity of pixels alpha layer

Post by snibgo »

Oops, sorry. In my second command, change "-black-threshold" to "-threshold".
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: find opacity of pixels alpha layer

Post 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.
bossrunner1
Posts: 8
Joined: 2017-03-16T05:00:27-07:00
Authentication code: 1151

Re: find opacity of pixels alpha layer

Post by bossrunner1 »

Thanks geemack.
This doesn't work here - red is still transparent.
I have IM 6.697
Thankyou
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: find opacity of pixels alpha layer

Post 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.
Post Reply