Perl GD Check If Pixel is Transparent

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 02:33:00

问题


I've been trying to solve this for a while. I have to check if a given pixel(x,y) is fully transparent.

1.How to Extract the alpha channel from a given pixel? Having an alpha channel of 127 will mean that the pixel is transparent? 2. I have tested the following code on a transparent pixel and It produces an RGB combination of a really dark(almost black) colour. I could use this as indicator, but I need a more accurate way.

my $myImage = newFromPng GD::Image($path);
$myImage->saveAlpha(1);
my $index = $myImage->getPixel($x,$y);
my ($red,$green,$blue) = $myImage->rgb($index);

回答1:


I found a solution that seems to work properly:

my $index = $myImage->getPixel($x,y); 

will return a colour palette. The color palette's range depends on the mode, the image is open in. If it is TrueColor(24-bit RGB-16,777,216 colors), which is the maximum amount of colours recognisable by the human eye and the maximum colours practically used, the maximum palette number will be 16,777,215. When the function is called on a "transparent" pixel, the number returned is over 2 billion which is an invalid number for a 24-bit RGB colour. So one simple check:

if ($index >= 1<<24) {
    #The pixel is transparent
}

did the trick for me.



来源:https://stackoverflow.com/questions/25121013/perl-gd-check-if-pixel-is-transparent

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!