How can I get the depth of a jpg file?

needthehelp picture needthehelp · Jan 3, 2010 · Viewed 14.8k times · Source

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.

Answer

Adam Rosenfield picture Adam Rosenfield · Jan 3, 2010

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]