0

I have problem with images bit depth. Does anybody can help or give some hint how to change bit depth of image from 32 bit to 24 bit or any other to 24 bit.

This is my code but this code convert images to 8 bit.

from PIL import Image

img=Image.open('iamge.png')
i=img.convert("P", palette=Image.Palette.ADAPTIVE, colors=8)
i.save('image.PNG')
petezurich
  • 9,280
  • 9
  • 43
  • 57
  • messing around with a palette is unlikely to be what you want. -- you need to consider what "bit depth" means. it can mean the depth of a single channel, which is usually 8 bits. or it could mean the total bits per pixel, which can be 8 (grayscale), 24 (RGB), or 32 (RGB + alpha channel). now, what operation do you need? if you don't know, show the data you have and what you need it for – Christoph Rackwitz Jun 26 '22 at 16:19
  • I need pictures for traffic signs recognition and my model works with 24 bit depth, if i put picture of 32 bit depth i got this error: ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 30, 30, 3), found shape=(None, 30, 30, 4) – Domagoj 954 Jun 26 '22 at 16:25
  • basically, call `.convert('RGB')` on the Image object. – Christoph Rackwitz Jun 26 '22 at 16:27
  • and yes i don't need alpha channel, i need 24 (RGB). – Domagoj 954 Jun 26 '22 at 16:28
  • if you're after some space savings, doing `.convert("RGB").convert("P")` might do it... and there's this: https://stackoverflow.com/questions/35859140/remove-transparency-alpha-from-any-image-using-pil – Christoph Rackwitz Jun 26 '22 at 16:30
  • Thanks for help, i will try your solutions later, im busy now, but i think that you are right. – Domagoj 954 Jun 26 '22 at 16:31
  • 1
    I tried now with .convert('RGB') and that's work fine, just what i need, thanks Mr. Rackwitz. – Domagoj 954 Jun 26 '22 at 19:45

0 Answers0