簡體   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