Reputation: 375
I am trying to convert a group of 24 bit-per-pixel bitmap files into 16 bit-per-pixel images.
After looking around it appeared that Python's Image Library (PIL) could do it but so far I have only been able to do 8 or 24 bit picture exports. I need five bits per color 5 bits per channel and 1 alpha bit. What method of converting should I use? Here is my code:
import os
from PIL import Image
file_list = []
location = "C:/bad_images"
os.chdir(location)
for file in os.listdir(location):
if file.endswith(".bmp"):
BMP_file = os.path.basename(file)
print(os.path.join("/mydir", file))
##im = Image.open(BMP_file).convert("RGB", colors = 256) ##Does not
work Nor does L
im = Image.open(BMP_file).convert("P", colors = 256)
im.save('converted/' + BMP_file)
The images this script spits out are 8 bit colors. I cannot find any examples of 16 bit format. Thanks!
Upvotes: 2
Views: 1748
Reputation: 146
For your code:
im = Image.open(BMP_file).convert("P", colors = 256)
im.save('converted/' + BMP_file)
Try using:
im = Image.open(BMP_file).convert("PA", colors = 256)
im.save('converted/' + BMP_file)
Or
im = Image.open(BMP_file).convert("PA;L", colors = 256)
im.save('converted/' + BMP_file)
“P” goes to 8 bit, but “PA” should give you 16. There is a list near the bottom of this file about the conversion letters to use. http://svn.effbot.org/public/pil/libImaging/Unpack.c
Also you’re asking for a 5x5x5x1 bit format which I’m not as familiar with, I have seen 5x6x5 or 4x4x4x4 format.
Upvotes: 1