繁体   English   中英

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

[英]How to determine if an image has a transparent pixel

我刚刚找到了一段代码,用于确定图像是否具有透明像素:

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

显然,这是行不通的。 我使用具有透明像素的打开图标库的图像user-desktop.png进行了尝试。 支票返回-1。

我只能猜测为什么使用此命令。 GD的手册说:

如果不带任何参数调用此方法[transparent],它将返回透明颜色的当前索引;如果没有,则返回-1。

因此,作为一个附带问题:透明像素完全不能具有颜色索引-对吗?

然后我发现线程Perl GD检查像素是否透明 但是对于这种解决方案,我必须遍历图像的所有像素(至少在麦汁中,当唯一的透明像素是最后一个像素时)。

是否有一种简便的方法来检查此信息? 也许像$ image_object-> has_transparent_pixels这样的方法?

注意:我不限于GD,因此其他Image模块也可以正常工作(但是,我在Windows上-应该在那里工作)。

更新的答案

正如@ikegami(谢谢您)所指出的那样,GIF在其调色板中具有指定的透明“彩色”像素,而不是alpha /透明层,其中每个像素都有其自己的透明度值,通常在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

作为一种替代方法,并且在各个平台之间的问题较少, %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不支持透明, 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM