I have images where I want to know the bit depth of the .bmp image. This is quite easy in windows manually (Properties>Details..) but there doesn't seem to much from google on it and the one answer I saw on here didn't (for me) show how to do it.
How to find the Bit Depth of an image
Code
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'])
or
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)
I can read the bit depth of a png but haven't a clue which library can get similar information so easily from a bmp.
You would probably be better off with a proper library, like
wand
orexiftool
, but if you want something lightweight, this might be good enough - but I can't test it on your images as you haven't shared any:I created a sample BMP with ImageMagick like this:
I then ran my script and got
bpp:16
matchingexiftool
output:I then created a 24-bit BMP like this:
and both my Python and
exiftool
report 24 bpp.Keywords: Python. BMP, image processing, get depth, bit-depth, bpp.