Skip to content

Transparent 1-bit PNG images treated as opaque (alpha component 255) #9281

@peteroupc

Description

@peteroupc

This library doesn't recognize transparent pixels as such in images loaded from 1-bit PNGs with a 'tRNS' chunk. The following example loads an 8×2 image, where the first row is transparent and the second row is black. Despite this, when retrieving a pixel from the first row, the alpha component is 255, and not 0 (transparent) as expected.

import base64
import io
import PIL.Image

# 8×2 image: first row transparent, second row black
bio=io.BytesIO(base64.b64decode(
  b'iVBORw0KGgoAAAANSUhEUgAAAAgAAAACAQAAAABN'+\
  b'76BAAAAAAnRSTlMAAQGU/a4AAAAMSURBVHicY/jPwAAAAwEBAMn+'+\
  b'ku8AAAAASUVORK5CYII='))
pi=PIL.Image.open(bio)
# Note: Image has mode '1' and a 'tRNS' chunk
pi2=pi.convert("RGBA")
# alpha component of first row's pixel should equal 0,
# but is 255 instead
print(pi2.getpixel([0,0]))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions