如何确定是否图像具有透明像素

如何确定是否图像具有透明像素

问题描述:

我只是发现了一块的代码,用于确定图像是否包含透明像素:如何确定是否图像具有透明像素

my $alpha = $gd->transparent; 
if ($alpha < 0) { 
    die("The image you uploaded has no transparent pixels. (alpha = $alpha)"); 
} 

显然,这是行不通的。我使用透明像素的open icon library的图像user-desktop.png尝试了它。检查返回-1。

我只能猜测为什么使用这个命令。 GD的说明书上说:

如果您在没有任何参数的情况下调用此方法[透明],它将返回透明颜色的当前索引,如果没有,则返回-1。

因此,作为一个侧面的问题:透明像素根本没有颜色索引 - 对吧?

然后我找到线程Perl GD check if pixel is transparent。但是对于这个解决方案,我必须遍历图像的所有像素(至少在麦汁情况下,当唯一的透明像素是最后一个时)。

是不是有一个简单的方法来检查这些信息?也许像$ image_object-> has_transparent_pixels的方法?

注意:我没有被绑定到GD,所以其他图像模块也可能工作(但是,我在Windows上 - 它应该在那里工作)。

更新回答

正如所指出的@ikegami(谢谢你),GIF格式具有指定透明“颜色”在他们的调色板像素,而不是其中每个像素具有其自己的透明度的α/透明度层值,通常在0-255之间。

我生成了一个具有一个透明像素,一个红色,一个绿色和一个蓝色的2x2像素GIF - 然后运行ImageMagick的identify命令,并得到以下结果。我用箭头标记了透明像素部分。

Image: a.gif 
    Format: GIF (CompuServe graphics interchange format) 
    Mime type: image/gif 
    Class: PseudoClass 
    Geometry: 4x4+0+0 
    Units: Undefined 
    Type: PaletteAlpha 
    Endianess: Undefined 
    Colorspace: sRGB 
    Depth: 8/1-bit 
    Channel depth: 
    red: 1-bit 
    green: 1-bit 
    blue: 1-bit 
    alpha: 1-bit 
    Channel statistics: 
    Pixels: 16 
    Red: 
     min: 0 (0) 
     max: 255 (1) 
     mean: 127.5 (0.5) 
     standard deviation: 127.5 (0.5) 
     kurtosis: -2 
     skewness: 0 
    Green: 
     min: 0 (0) 
     max: 255 (1) 
     mean: 127.5 (0.5) 
     standard deviation: 127.5 (0.5) 
     kurtosis: -2 
     skewness: 0 
    Blue: 
     min: 0 (0) 
     max: 255 (1) 
     mean: 127.5 (0.5) 
     standard deviation: 127.5 (0.5) 
     kurtosis: -2 
     skewness: 0 
    Alpha: 
     min: 0 (0) 
     max: 255 (1) 
     mean: 191.25 (0.75) 
     standard deviation: 110.418 (0.433013) 
     kurtosis: -0.666667 
     skewness: 1.1547 
    Image statistics: 
    Overall: 
     min: 0 (0) 
     max: 255 (1) 
     mean: 111.562 (0.4375) 
     standard deviation: 123.451 (0.484123) 
     kurtosis: -1.8275 
     skewness: 0.271109 
    Alpha: srgba(255,255,255,0) #FFFFFF00 
    Colors: 4 
    Histogram: 
     4: ( 0, 0,255,255) #0000FF blue 
     4: ( 0,255, 0,255) #00FF00 lime 
     4: (255, 0, 0,255) #FF0000 red 
     4: (255,255,255, 0) #FFFFFF00 srgba(255,255,255,0) 
    Colormap entries: 4 
    Colormap: 
     0: (255, 0, 0,255) #FF0000 red 
     1: ( 0,255, 0,255) #00FF00 lime 
     2: ( 0, 0,255,255) #0000FF blue 
     3: (255,255,255, 0) #FFFFFF00 srgba(255,255,255,0)  <--------- 
    Rendering intent: Perceptual 
    Gamma: 0.454545 
    Chromaticity: 
    red primary: (0.64,0.33) 
    green primary: (0.3,0.6) 
    blue primary: (0.15,0.06) 
    white point: (0.3127,0.329) 
    Background color: srgba(255,255,255,0) 
    Border color: srgba(223,223,223,1) 
    Matte color: grey74 
    Transparent color: srgba(255,255,255,0) <--------------- 
    Interlace: None 
    Intensity: Undefined 
    Compose: Over 
    Page geometry: 4x4+0+0 
    Dispose: Undefined 
    Compression: LZW 
    Orientation: Undefined 
    Properties: 
    date:create: 2014-10-11T10:40:09+01:00 
    date:modify: 2014-10-11T10:40:08+01:00 
    signature: 1c82b4c2e772fb075994516cc5661e9dec35b8142f89c651253d07fc3c4642bb 
    Profiles: 
    Profile-gif:xmp dataxmp: 1031 bytes 
    Artifacts: 
    filename: a.gif 
    verbose: true 
    Tainted: False 
    Filesize: 1.11KB 
    Number pixels: 16 
    Pixels per second: 16PB 
    User time: 0.000u 
    Elapsed time: 0:01.000 
    Version: ImageMagick 6.8.9-7 Q16 x86_64 2014-09-10 http://www.imagemagick.org 

那么,IM不知道的GIF透明像素 - 我将挖掘更多一些,看看它是否能够明智地采用Perl - 不过现在,你可以只运行Perl的反引号以下。

my $output = `identify -verbose a.gif | grep -i transparent`; # or maybe with FINDSTR on Windows 

作为alternatvie,以及跨平台问题较少,在%A逃逸告诉你,如果图像已启用透明度:

convert a.gif -print "%A" null: 
True 

convert a.jpg -print "%A" null: 
False 

或者,更Perl的-Y方式:

#!/usr/bin/perl 
use warnings; 
use strict; 
use Image::Magick; 
my $image = Image::Magick->new(); 
$image->Read($ARGV[0]); 
my $a = $image->Get('%A'); 
print $a; 

perl ./script.pl a.gif 
True 

perl ./script.pl a.jpg 
False 

原创回答

我想你可能对透明度有点困惑。图像要么具有透明度,要么是整个图层,要么没有。一般来说,这不是单个像素透明或不透明的问题。从一开始,JPEG s不支持透明度,GIFPNG可以支持透明度,但它们不一定是总是透明。

因此,假设您有一个PNGGIF,它可以有一个透明度层。如果有的话,每个像素可以是完全透明的,完全不透明的或者介于两者之间的。如果您使用ImageMagick,它可以在命令行或PHP,Perl和其他绑定中使用。

在命令行中,你可以告诉如果图像具有使用此命令透明层:

convert InputImage.png -format "%[opaque]" info: 

,它或者返回truefalse

在Perl中,你可以这样做:

#!/usr/bin/perl 
use warnings; 
use strict; 
use Image::Magick; 
my $image = Image::Magick->new(); 
$image->Read($ARGV[0]); 
my $a = $image->Get('%[opaque]'); 
print $a; 

然后根据运行:

perl ./script.pl ImageName.png 
+1

为什么在一个Perl问题所有.NET谈话? – ThisSuitIsBlackNot 2014-10-10 18:42:27

+1

@ThisSuitIsBlackNot Oops,我看到了Windows,可能已经开始了一个星期五晚上的酒杯 - 现在会更新... – 2014-10-10 18:50:54

+1

“如果有,每个像素可以是完全透明的,完全不透明的或者介于两者之间的。这不是真的。你对OP的透明度同样困惑。有两种透明度机制。 OP只知道其中的一个,而你只知道另一个。 GIF使用OP和'$ gd-> transparent'引用的机制。 GIF使用(最多)256色的调色板。这256个索引之一可以被指定为透明色。这种颜色的像素是100%透明的。其他颜色的像素是100%不透明的。 – ikegami 2014-10-11 09:16:30