How can I get the depth of a jpg file?

南楼画角 提交于 2019-12-08 19:18:38

问题


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.


回答1:


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]



回答2:


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.




回答3:


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.




回答4:


I was going to say that JPG images are 24 bit by definition. They normally consist of three 8 bit colour channels, one for each of red, green and blue making 24 bits per pixel. However, I've just found this page which states:

If you use a more modern version of Photoshop, you'll notice it will also let you work in 16-bits per channel, which gives you 48 bits per pixel.

But I can't find a reference for how you'd tell the two apart.



来源:https://stackoverflow.com/questions/1996577/how-can-i-get-the-depth-of-a-jpg-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!