Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the depth of a jpg file?

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.

like image 989
needthehelp Avatar asked Jan 03 '10 22:01

needthehelp


People also ask

How do I convert a JPG file to a 24-bit 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.

Is JPEG 24-bit depth?

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).


3 Answers

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]
like image 147
Adam Rosenfield Avatar answered Oct 09 '22 18:10

Adam Rosenfield


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.

like image 25
Greg Hewgill Avatar answered Oct 09 '22 19:10

Greg Hewgill


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.

like image 33
Mike Avatar answered Oct 09 '22 20:10

Mike