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?".
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 »

snibgo wrote:Oops, sorry. In my second command, change "-black-threshold" to "-threshold".
That does what you want, does 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 »

This works totally brilliantly and just the way i wanted it to.
It is fast and work !!
Thank you for the whole team that has helped me so brilliantly.
Appreciate it a lot
Matt :) :) :)
mjamal
Posts: 62
Joined: 2017-12-27T06:13:59-07:00
Authentication code: 1152

Re: find opacity of pixels alpha layer

Post by mjamal »

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
From the above code, can we remove the pixels from the image which are having less then 30% opacity instead of making color RED?
My requirement is like I am getting image which are having may pixels which are having zero opacity and I need to remove all the pixels from the image and can create a new image which will not have all the pixels which are having 0 opacity.

Thanks in advanced.
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 »

wrote:... can we remove the pixels from the image ...
What does "remove" mean? Do you want to make them fully transparent? Or do you want to crop the image, trimming those pixels from edges? Or what?

Sample images may help. Also say what version of IM you use, on what platform.
snibgo's IM pages: im.snibgo.com
mjamal
Posts: 62
Joined: 2017-12-27T06:13:59-07:00
Authentication code: 1152

Re: find opacity of pixels alpha layer

Post by mjamal »

Hi Snibgo,

Thanks for your quick reply.

Remove means we need to make fully transparent those pixels or we it will be good if we can crop those pixels from the image ie Yes for (trimming those pixels from edges).

You can see the sample image from below URL.
https://www.dropbox.com/s/urqh6u9wigodo ... 8.png?dl=0

And I am using the Linux platform and IM version is ImageMagick 6.7.8-9
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 »

That image has only two values of alpha: zero (fully transparent) and 100% (fully opaque).

We can trim the fully transparent pixels with:

Code: Select all

convert 5bd84de35c7c8.png -bordercolor None -border 1 -trim +repage out.png
Your version of IM is very old. I suggest you upgrade it.
snibgo's IM pages: im.snibgo.com
mjamal
Posts: 62
Joined: 2017-12-27T06:13:59-07:00
Authentication code: 1152

Re: find opacity of pixels alpha layer

Post by mjamal »

Code: Select all

convert 5bd84de35c7c8.png -bordercolor None -border 1 -trim +repage out.png
Hi Snigbo,

I have tried with the above command but still the output result is having the pixels which are having value as 0,0,0,0 when I checked the histogram info of the output image from the below command.

exec("convert out.png -format %c histogram:info:outTxT.txt");

You can see the histogram info file from the below URL and we need to remove the pixel values (from the output image) present in first line "158548: ( 0, 0, 0, 0) #00000000 none" from the below txt file.
https://www.dropbox.com/s/v021gew8n2p10 ... T.txt?dl=0

Please provide any feedback/suggestion.
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 »

"-trim" removes edges to that no edge has all pixels the same colour, in this case transparent-black.

Half the pixels in your image are transparent-black. You want to remove them all, so the result will be much smaller than the input. Is that right?

Perhaps you can make an example (eg with Gimp) of what you want the result to look like.

There are many ways to do that, each giving different results, ie each trimming to a different rectangle that contains no transparent-black pixels. See my Inner Trim page, or Fred's "InnerCrop" bash script.
snibgo's IM pages: im.snibgo.com
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 »

There is no way to remove all transparency from this type of irregularly shaped image apart from changing transparency to some opaque color. If you trim the outer parts, there will still be some transparency. Only an unrotated rectangular object can be trimmed to remove all transparency. I do not see any inner trim like approach to work either in this type of image.

Please post a result of what you would like to see as your result. You can use Photoshop or GIMP.
mjamal
Posts: 62
Joined: 2017-12-27T06:13:59-07:00
Authentication code: 1152

Re: find opacity of pixels alpha layer

Post by mjamal »

Hi Fred,

Thank you for your feedback.

Below is the URL for the source image (demo_selectionbb.png) which is using for cropping via script and Photoshop.
https://www.dropbox.com/s/9a9cfdrpe6jxs ... b.png?dl=0

URL for the output image which is cropped from the image magick script.
https://www.dropbox.com/s/5etfq7lmioepx ... t.png?dl=0
And image magick script for cropping is given below.

Code: Select all

exec('convert uploads/demo_selectionbb.png -background white -alpha background \( +clone -fill black -colorize 100 -fill white +antialias -draw "polygon 99,24 63,215 30,372 1,535 15,575 48,575 101,560 121,523 136,467 163,376 192,310 227,231 269,120 300,126 342,277 369,371 435,516 500,600 548,591 548,548 533,441 519,307 509,226 483,127 471,70 457,16 409,22 357,23 313,22 309,51 296,71 276,79 266,40 267,18 227,1 209,0 202,5 197,14 173,23 142,25 108,20" -alpha off \) -alpha off -compose copy_opacity -composite IM_result.png'); 
Histogram info for the image which is cropped from the image magick script.
https://www.dropbox.com/s/sdrp8hzbk26lp ... t.txt?dl=0

URL for the output image which is cropped from the Photoshop software.
https://www.dropbox.com/s/h4t63iejlp0lv ... t.png?dl=0

Histogram info for the image which is cropped from Photoshop.
https://www.dropbox.com/s/firfqvhtde3k6 ... t.txt?dl=0

You can see the difference in both the histograms txt files and we need to remove the below pixels which are present in image magick output image's txt file (IM_result.txt).

157903: ( 0, 0, 0, 0) #00000000 none
1045: ( 0, 0, 0,255) #000000 black

Your suggestions are mostly welcome.
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 »

Please post the actual PSD file. Your image has a checkerboard background. Photoshop puts a checkerboard where there is transparency, though that is not in the image. If you export the PSD file as PNG, you will see that it then contains transparency and you will get those values in the histogram.

If the screen snap with the checkerboard from PSD is what you are using to get the histogram, it is an unfair comparison.

If you want IM to put the checkerboard behind the pants, then you can add a checkerboard background with ImageMagick and then flatten the transparent image against the checkerboard.

try this if you want a checkerboard behind the image.

Code: Select all

convert \( demo_selection.png +repage -background black -alpha background \
\( +clone -fill black -colorize 100 -fill white +antialias \
-draw "polygon 99,24,63,215,30,372,1,535,15,575,48,575,101,560,121,523,136,467,163,376,192,310,227,231,269,120,300,126,342,277,369,371,435,516,500,600,548,591,548,548,533,441,519,307,509,226,483,127,471,70,457,16,409,22,357,23,313,22,309,51,296,71,276,79,266,40,267,18,227,1,209,0,202,5,197,14,173,23,142,25,108,20" -alpha off \) \
-alpha off -compose copy_opacity -composite \) \
\( pattern:checkerboard -resize 50x50 -write mpr:checks +delete \
+clone -tile mpr:checks -draw "color 0,0 reset" \) \
+swap -compose over -flatten result.png 
If you want one long command line, then remove only the end of line \. So

Code: Select all

convert \( demo_selection.png +repage -background black -alpha background \( +clone -fill black -colorize 100 -fill white +antialias -draw "polygon 99,24,63,215,30,372,1,535,15,575,48,575,101,560,121,523,136,467,163,376,192,310,227,231,269,120,300,126,342,277,369,371,435,516,500,600,548,591,548,548,533,441,519,307,509,226,483,127,471,70,457,16,409,22,357,23,313,22,309,51,296,71,276,79,266,40,267,18,227,1,209,0,202,5,197,14,173,23,142,25,108,20" -alpha off \) -alpha off -compose copy_opacity -composite \) \( pattern:checkerboard -resize 50x50 -write mpr:checks +delete +clone -tile mpr:checks -draw "color 0,0 reset" \) +swap -compose over -flatten result.png 
Post Reply