Black pixels in some PNG images become transparent

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
Jason S
Posts: 103
Joined: 2010-12-14T19:42:12-07:00
Authentication code: 8675308

Black pixels in some PNG images become transparent

Post by Jason S »

Start with an 8-bit RGBA PNG image in which all the pixels are fully opaque, and all the pixels are shades of gray, some of which are black. Run it through 'convert':
convert in.png out.png

My result: The output image is an 8-bit grayscale image with no alpha channel, and with a 'tRNS' chunk that makes the black pixels transparent.
Expected result: The output image should have no transparent pixels.
ImageMagick version: 6.6.6-7
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Black pixels in some PNG images become transparent

Post by fmw42 »

this also shows a similar problem on IM 6.6.6.7 Q16 (hdri) Mac OSX Tiger:

convert -size 256x256 gradient: grad256.png
identify -verbose grad256.png

Image: grad256.png
Format: PNG (Portable Network Graphics)
Class: DirectClass
Geometry: 256x256+0+0
Resolution: 72x72
Print size: 3.55556x3.55556
Units: Undefined
Type: Grayscale
Base type: Grayscale
Endianess: Undefined
Colorspace: RGB
Depth: 8-bit
Channel depth:
gray: 8-bit
Channel statistics:
Gray:
min: 0 (0)
max: 255 (1)
mean: 127.5 (0.5)
standard deviation: 73.9003 (0.289805)
kurtosis: -1.20004
skewness: 1.26355e-12
Histogram:
256: ( 0, 0, 0) #000000 black
256: ( 1, 1, 1) #010101 rgb(1,1,1)
256: ( 2, 2, 2) #020202 rgb(2,2,2)
256: ( 3, 3, 3) #030303 grey1
256: ( 4, 4, 4) #040404 rgb(4,4,4)
256: ( 5, 5, 5) #050505 grey2
256: ( 6, 6, 6) #060606 rgb(6,6,6)
256: ( 7, 7, 7) #070707 rgb(7,7,7)


convert grad256.png -alpha opaque grad256op.png
identify -verbose grad256op.png

Image: grad256op.png
Format: PNG (Portable Network Graphics)
Class: DirectClass
Geometry: 256x256+0+0
Resolution: 72x72
Print size: 3.55556x3.55556
Units: Undefined
Type: GrayscaleMatte
Base type: GrayscaleMatte
Endianess: Undefined
Colorspace: Gray
Depth: 8-bit
Channel depth:
gray: 8-bit
alpha: 1-bit
Channel statistics:
Gray:
min: 0 (0)
max: 255 (1)
mean: 127.5 (0.5)
standard deviation: 73.9003 (0.289805)
kurtosis: -1.20004
skewness: 1.26355e-12
Alpha:
min: 0 (0)
max: 255 (1)

mean: 254.004 (0.996094)
standard deviation: 15.9063 (0.0623778)
kurtosis: 251.004
skewness: 15.9061
Alpha: none #00000000
Histogram:
256: ( 0, 0, 0, 0) #00000000 none
256: ( 1, 1, 1,255) #010101 graya(1,1,1,1)
256: ( 2, 2, 2,255) #020202 graya(2,2,2,1)
256: ( 3, 3, 3,255) #030303 grey1
256: ( 4, 4, 4,255) #040404 graya(4,4,4,1)
256: ( 5, 5, 5,255) #050505 grey2
256: ( 6, 6, 6,255) #060606 graya(6,6,6,1)
256: ( 7, 7, 7,255) #070707 graya(7,7,7,1)
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Black pixels in some PNG images become transparent

Post by anthony »

This is an on going problem with the new PNG coder for the automatic handling of images with less than 256 colors in the image.

One temporary solution is to force it into a non indexed PNG format using png32:filename.png
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Black pixels in some PNG images become transparent

Post by glennrp »

The PNG decoder sets image->matte to True when it reads an RGBA image, regardless of whether any transparent pixel is actually present. The PNG encoder writes a tRNS chunk when it inherits a grayscale or truecolor image with image->matte==MagickTrue. I'm working on a fix for this.
macias
Posts: 28
Joined: 2008-12-10T13:44:19-07:00

Re: Black pixels in some PNG images become transparent

Post by macias »

I can confirm this with such info about IM versions:

IM 6.6.5-4 -- didn't happen for me
IM 6.6.6-4 -- I spotted this issue for the first time
IM 6.6.6-8 -- bug still exists
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Black pixels in some PNG images become transparent

Post by glennrp »

Works in SVN head and should work soon in IM-6.6.6-9. The problem was that the input image was RGBA without any transparent pixels. Now the decoder detects this situation and doesn't set image->matte.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Black pixels in some PNG images become transparent

Post by fmw42 »

Glenn,

This is still not working in IM 6.6.6.9 Q16 (hdri) Mac OSX Tiger. Perhaps it is not really in this release, but will be in the next.


convert grad256.png -alpha opaque grad256op.png
identify -verbose grad256op.png

Image: grad256op.png
Format: PNG (Portable Network Graphics)
Class: DirectClass
Geometry: 256x256+0+0
Resolution: 72x72
Print size: 3.55556x3.55556
Units: Undefined
Type: GrayscaleMatte
Base type: GrayscaleMatte
Endianess: Undefined
Colorspace: Gray
Depth: 8-bit
Channel depth:
gray: 8-bit
alpha: 1-bit
Channel statistics:
Gray:
min: 0 (0)
max: 255 (1)
mean: 127.5 (0.5)
standard deviation: 73.9003 (0.289805)
kurtosis: -1.20004
skewness: 1.26355e-12
Alpha:
min: 0 (0)

max: 255 (1)
mean: 254.004 (0.996094)
standard deviation: 15.9063 (0.0623778)
kurtosis: 251.004
skewness: 15.9061
Alpha: none #00000000
Histogram:
256: ( 0, 0, 0, 0) #00000000 none
256: ( 1, 1, 1,255) #010101 graya(1,1,1,1)
256: ( 2, 2, 2,255) #020202 graya(2,2,2,1)
256: ( 3, 3, 3,255) #030303 grey1
256: ( 4, 4, 4,255) #040404 graya(4,4,4,1)
256: ( 5, 5, 5,255) #050505 grey2
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Black pixels in some PNG images become transparent

Post by glennrp »

fmw42 wrote: This is still not working in IM 6.6.6.9 Q16 (hdri) Mac OSX Tiger. Perhaps it is not really in this release, but will be in the next.
Right. 6-9 was created shortly before I checked in the fix. Try again with 6-10.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Black pixels in some PNG images become transparent

Post by fmw42 »

glennrp wrote:
fmw42 wrote: This is still not working in IM 6.6.6.9 Q16 (hdri) Mac OSX Tiger. Perhaps it is not really in this release, but will be in the next.
Right. 6-9 was created shortly before I checked in the fix. Try again with 6-10.

Still does not work correctly in 6.6.6.10 Q16 (hdri) Mac OSX Tiger

(note it may be related to viewtopic.php?f=3&t=17771 )


convert -size 256x256 gradient: grad256.png
convert grad256.png -alpha opaque grad256op.png
identify -verbose grad256op.png

Image: grad256op.png
Format: PNG (Portable Network Graphics)
Class: DirectClass
Geometry: 256x256+0+0
Resolution: 72x72
Print size: 3.55556x3.55556
Units: Undefined
Type: GrayscaleMatte
Base type: GrayscaleMatte
Endianess: Undefined
Colorspace: Gray
Depth: 8-bit
Channel depth:
gray: 8-bit
alpha: 1-bit
Channel statistics:
Gray:
min: 0 (0)
max: 255 (1)
mean: 127.5 (0.5)
standard deviation: 73.9003 (0.289805)
kurtosis: -1.20004
skewness: 1.26355e-12
Alpha:
min: 0 (0)
max: 255 (1)
mean: 254.004 (0.996094)
standard deviation: 15.9063 (0.0623778)
kurtosis: 251.004
skewness: 15.9061
Alpha: none #00000000
Histogram:
256: ( 0, 0, 0, 0) #00000000 none
256: ( 1, 1, 1,255) #010101 graya(1,1,1,1)
256: ( 2, 2, 2,255) #020202 graya(2,2,2,1)
256: ( 3, 3, 3,255) #030303 grey1
256: ( 4, 4, 4,255) #040404 graya(4,4,4,1)
256: ( 5, 5, 5,255) #050505 grey2
Post Reply