Skip to content

Added bit_depth attribute to PngImageFile#9601

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

Added bit_depth attribute to PngImageFile#9601
ajslater wants to merge 4 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?

@ajslater
Copy link
Copy Markdown
Author

ajslater commented May 5, 2026

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

In my application, called picopt, I do PNG lossless optimization. The bit depth detection code I have is actually used to disqualify high bit depth pngs from using the PngOut optimize which doesn't accept them. I've had this code rattling around my app for a while now and always wondered if PIllow users might find this extra information useful.

Idk if it really fits what Pillow wants to accomplish, but I thought I'd run it by you.

I just tested your solution using _MODES and it works in my code.
A "bit_depth" info key would be a little more convenient, of course, for consumers. If that sounds useful to you I could populate that value any which way.

Or are you just saying that you're willing to accept feedback?

Yes, that. I've never contributed to Pillow before and was unsure if this PR met the standards and practices of this repo.

I suppose the questions are:

  1. Would a conveniently accessible bit_depth value be useful for Pillow users or would it just be expanding the info API needlessly?
  2. If it is wanted, what method would you prefer to extract the data?
  3. If not, would your code snippet be useful to people in the docs? It was interesting to me. I didn't know you could do that.

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