Skip to content

Added bit_depth attribute to PngImageFile#9601

Open
ajslater wants to merge 3 commits intopython-pillow:mainfrom
ajslater:feature/png-bit-depth
Open

Added bit_depth attribute to PngImageFile#9601
ajslater wants to merge 3 commits intopython-pillow:mainfrom
ajslater:feature/png-bit-depth

Conversation

@ajslater
Copy link
Copy Markdown

@ajslater ajslater commented May 1, 2026

I find it useful when working with PNGs to know their bit depth and since i use Pillow to detect image formats as well as manipulate images I thought it might be a useful addition.

This just reads a well known byte from the png stream and stores it on the PngImageFile.

Tell me if this looks useful and I can modify this PR to work well as a Pillow contribution.

@radarhere
Copy link
Copy Markdown
Member

radarhere commented May 2, 2026

I find it useful when working with PNGs to know their bit depth

Could you elaborate on this? Pillow attempts to deal with this kind of low level information so you don't have to. You should be able to infer a less-precise bit depth from im.mode, or you can look at im.png.im_rawmode and figure out the exact value.

The following code would let you determine a numeric value for the bit depth at the moment.

from PIL import Image, PngImagePlugin
with Image.open("Tests/images/hopper.png") as im:
    for bitDepth_colorType, mode_rawmode in PngImagePlugin._MODES.items():
        if im.png.im_rawmode == mode_rawmode[1]:
            print("Bit depth:", bitDepth_colorType[0])

I acknowledge my suggestion is inelegant and uses a private property.

If we did go with your suggestion, I think it would be better as another key in self.info, rather than a separate property.

Tell me if this looks useful and I can modify this PR to work well as a Pillow contribution.

I find this comment confusing. What do you think is undone about this PR? Or are you just saying that you're willing to accept feedback?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants