#!/usr/bin/python3 # -*- coding: utf-8 -*- import sys from PIL import Image rgb332_palette=[] #RGB palette generation: Red(3bits) Green(3bits) Blue(2bits)/1pixel = 256 colors (as used in .coe VGA mem files) for i in range(256): rgb332_palette.append(int(((i & 224) >> 5)*(255/7))) # & 224 means a bit mask for the MSB and >> 5 is a bitwise to strip LSB bits rgb332_palette.append(int(((i & 28) >> 2)*(255/7))) rgb332_palette.append(int((i & 3)*(255/3))) img = Image.open(sys.argv[1]) img.show() #print(img.format+' '+img.mode+' '+str(img.size) + str(img.info)) #for rgb332 bmp:rom64_p11.bmp (previously created by me with testcoe.py, reading coefile: rom64_p11.coe) if type(list(img.getdata())[0]) == type(int()): imgrgbbytes_plain=''.join([format(i, '02x').upper() for i in list(img.getdata())]) im2 = Image.frombytes('P', img.size, bytes.fromhex(imgrgbbytes_plain)) im2.putpalette(rgb332_palette) #without palette I get b/n like img im2.show() #for generic 24bits RGB bmp: Lenna.bmp else: imgrgbbytes = [] for rgb in list(img.getdata()): for item in rgb: imgrgbbytes.append(item) print(imgrgbbytes) imgrgbbytes_plain=''.join([format(i, '02x').upper() for i in imgrgbbytes]) im2 = Image.frombytes('P', img.size, bytes.fromhex(imgrgbbytes_plain)) #im2.putpalette(rgb332_palette) #without palette I get b/n like img im2.show() im3=Image.new('P',img.size) im3.putpalette(rgb332_palette) #without palette I get b/n like img with palette blue tone im3.paste(img) im3.show()