Problem obtaining alpha channel of pngquant/tinypng files.

조회 수: 3 (최근 30일)
Ernest Woei
Ernest Woei 2017년 2월 13일
댓글: Walter Roberson 2017년 2월 22일
I am having trouble obtaining the alpha channel of a pngquant/tinypng file using imread. Is there another function that handles this properly?
Used:
[img,imgMap,imgAlpha] = imread('some_pngquant_file.png');
img (mxn uint8 matrix), imgMap (256x3 double matrix with values in [0,1]), imgAlpha is empty
img entries index into imgMap rows (RGB values in [0,1]). Could it be that I'm interpreting this improperly?
  댓글 수: 2
Saurabh Gupta
Saurabh Gupta 2017년 2월 17일
The imread documentation says "For PNG files, transparency is the alpha channel, if present. If no alpha channel is present, ... then transparency is []."
Are you sure your image file has an alpha channel?
Ernest Woei
Ernest Woei 2017년 2월 21일
Yes, unless the image viewer is creating a transparency layer on its own, which I doubt.
Upon opening the pngquant'ed file in Photoshop. I can view the transparency layer, i.e., select layer, Menu: Layer -> Layer Mask -> From Transparency, then viewing the mask channel through the 'Channels' tab.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2017년 2월 17일
편집: Walter Roberson 2017년 2월 22일
The sample "Firefox logo" image I found that had been created by the open source library did not have an Alpha channel. Instead, imfinfo says that 'Transparency' is 'simple' and there is a SimpleTransparencyData property that appears to give some kind of alpha mapping. The PNG specification does not permit true Alpha channels for indexed (pseudocolor) images.
Looking at http://www.libpng.org/pub/png/book/chapter08.html#png.ch08.div.5.2 the implication is that all entries past the end of the provided SimpleTransparencyData are the maximum (fully opaque.)
This leads to:
filename = 'NameOftinyPNG.png';
minfo = imfinfo(filename);
ACHAN = mfinfo.SimpleTransparencyData;
ACHAN(end+1:256) = 1;
[img, imgMap] = imread(filename);
AMAP = ACHAN(double(img)+1);
image(img, 'AlphaData', AMAP);
colormap(imgMap);
  댓글 수: 3
Ernest Woei
Ernest Woei 2017년 2월 21일
Web Logo:
Web Logo pngquant
Walter Roberson
Walter Roberson 2017년 2월 22일
Thanks, I have corrected my typo for future reference.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by