python 如何获取 jpg 文件的深度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1996577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the depth of a jpg file?
提问by needthehelp
I want to retrieve the bit depth for a jpeg file using Python.
我想使用 Python 检索 jpeg 文件的位深度。
Using the Python Imaging Library:
使用 Python 图像库:
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?
但是,对于明显的 24 位图像,这给了我 8 的深度。难道我做错了什么?有没有办法用纯 Python 代码做到这一点?
Thanks in advance.
提前致谢。
Edit: It's data.bits not data.depth.
编辑:这是 data.bits 不是 data.depth。
回答by Adam Rosenfield
I don't see the depth
attribute documented anywhere in the Python Imaging Library handbook. However, it looks like only a limited number of modesare supported. You could use something like this:
我depth
在Python Imaging Library handbook中的任何地方都没有看到该属性的记录。但是,看起来只支持有限数量的模式。你可以使用这样的东西:
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]
回答by Greg Hewgill
Jpeg files don't havebit 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.
Jpeg 文件不像 GIF 或 PNG 文件那样具有位深度。用于创建 Jpeg 数据的变换在解压缩时呈现连续的色谱。
回答by Mike
回答by ChrisF
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 pagewhich states:
我想说 JPG 图像根据定义是 24 位的。它们通常由三个 8 位颜色通道组成,每个红色、绿色和蓝色通道一个,每个像素为 24 位。但是,我刚刚找到了这个页面,其中指出:
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.
如果您使用更现代的 Photoshop 版本,您会注意到它还能让您在每通道 16 位的情况下工作,这为您提供每像素 48 位。
But I can't find a reference for how you'd tell the two apart.
但是我找不到您如何区分两者的参考。
回答by Steven Moseley
I think this is what you're asking for... you can use the following to get the count of colors used in a JPG or other RGB or RGBA image
我认为这就是您要的......您可以使用以下内容来获取 JPG 或其他 RGB 或 RGBA 图像中使用的颜色计数
I personally use a variation on this code to determine if it makes sense to convert from RGB to an indexed mode.
我个人使用此代码的变体来确定从 RGB 转换为索引模式是否有意义。
im = Image.open('/path/to/file.jpg')
depth = len(set(im.getdata()))