I want to retrieve the bit depth for a jpeg file using Python.
Using the Python Imaging Library:
import Image
data = Image.open('file.jpg')
print data.depth
However, this gives me a depth of 8 for an obviously 24-bit image. Am I doing something wrong? Is there some way to do it with pure Python code?
Thanks in advance.
Edit: It's data.bits not data.depth.
Any color JPEG has 24 bit as input and when it is decompressed it is 24 bit again. You can create a full 24 bit image from a JPEG by converting it to PNG for example.
JPEG is an '8-bit' format in that each color channel uses 8-bits of data to describe the tonal value of each pixel. This means that the three color channels used to make up the photo (red, green and blue) all use 8-bits of data – so sometimes these are also called 24-bit images (3 x 8-bit).
I don't see the depth
attribute documented anywhere in the Python Imaging Library handbook. However, it looks like only a limited number of modes are supported. You could use something like this:
mode_to_bpp = {'1':1, 'L':8, 'P':8, 'RGB':24, 'RGBA':32, 'CMYK':32, 'YCbCr':24, 'I':32, 'F':32}
data = Image.open('file.jpg')
bpp = mode_to_bpp[data.mode]
Jpeg files don't have bit depth in the same manner as GIF or PNG files. The transform used to create the Jpeg data renders a continuous color spectrum on decompression.
PIL is reporting bit depth per "band". I don't actually see depth
as a documented property in the PIL docs, however, I think you want this:
data.depth * len(data.getbands())
Or better yet:
data.mode
See here for more info.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With