微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何获取位图图像的位深度

如何解决如何获取位图图像的位深度

我有一些图像,我想知道 .bmp 图像的位深度。这在 Windows 中手动很容易(属性>详细信息..),但谷歌似乎没有太多内容,我在这里看到的一个答案(对我来说)没有展示如何做到这一点。

How to find the Bit Depth of an image

代码

import png as png
import numpy as np

r=png.Reader(filename = r'C:\Users\priper\Desktop\OPW_refac\grayscale.png')
a = r.read()

print(a[3]['bitdepth'])

from PIL import Image
import numpy as np

#Load the BMP file    
img = Image.open(r'C:\Users\priper\Desktop\OPW_refac\HSS All As.bmp')
print(img,'\n')
print('bit depth :',img.mode)#this only tells me it is 8 pixels,I don't think it Could tell me if it was 4.

#Or as a numpy array
img = np.array(Image.open(r'C:\Users\priper\Desktop\OPW_refac\HSS All As.bmp'))
print(img)

我可以读取 png 的位深度,但不知道哪个库可以如此轻松地从 bmp 获取类似信息。

解决方法

使用合适的库(例如 wandexiftool)可能会更好,但是如果您想要轻量级的东西,这可能已经足够了 - 但我无法在您的图像上进行测试因为您还没有分享任何内容:

#!/usr/bin/env python3

import sys
import struct

# Read first 100 bytes
with open('a.bmp','rb') as f:
    BMP = f.read(100)

if BMP[0:2] != b'BM':
   sys.exit('ERROR: Incorrect BMP signature')

# Get BITMAPINFOHEADER size - https://en.wikipedia.org/wiki/BMP_file_format
BITMAPINFOHEADERSIZE = struct.unpack('<i',BMP[14:18])[0]
okSizes = [40,52,56,108,124]
if BITMAPINFOHEADERSIZE not in okSizes:
   sys.exit(f'ERROR: BITMAPINFOHEADER size was {BITMAPINFOHEADERSIZE},expected one of {okSizes}')

# Get bits per pixel
bpp = struct.unpack('<H',BMP[28:30])[0]
print(f'bbp: {bpp}')

我使用 ImageMagick 创建了一个示例 BMP,如下所示:

magick -size 32x32 xc:red -define bmp:subtype=RGB565  a.bmp

然后我运行我的脚本并得到 bpp:16 匹配的 exiftool 输出:

exiftool a.bmp

ExifTool Version Number         : 12.00
File Name                       : a.bmp
Directory                       : .
File Size                       : 2.1 kB
File Modification Date/Time     : 2021:02:24 12:01:51+00:00
File Access Date/Time           : 2021:02:24 12:01:52+00:00
File Inode Change Date/Time     : 2021:02:24 12:01:51+00:00
File Permissions                : rw-r--r--
File Type                       : BMP
File Type Extension             : bmp
MIME Type                       : image/bmp
BMP Version                     : Windows V5
Image Width                     : 32
Image Height                    : 32
Planes                          : 1
Bit Depth                       : 16           <--- HERE IT IS
Compression                     : Bitfields
Image Length                    : 2048
Pixels Per Meter X              : 0
Pixels Per Meter Y              : 0
Num Colors                      : Use BitDepth
Num Important Colors            : All
Red Mask                        : 0x0000f800
Green Mask                      : 0x000007e0
Blue Mask                       : 0x0000001f
Alpha Mask                      : 0x00000000
Color Space                     : sRGB
Rendering Intent                : Picture (LCS_GM_IMAGES)
Image Size                      : 32x32
Megapixels                      : 0.001

然后我创建了一个 24 位 BMP,如下所示:

magick -size 32x32 xc:red a.bmp

我的 Python 和 exiftool 报告为 24 bpp。

关键字:Python。 BMP,图像处理,获取深度,位深度,bpp。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。