from PIL import Image import numpy as np # Create an RGB pixel array (example) width = 512 height = 1024 rgb = np.zeros((height, width, 3), dtype=np.uint8) f = open("bmp.pal", "rb") for i in range(512): rd = f.read(3) r = rd[0] g = rd[1] b = rd[2] r = r & 0xE0 g = g & 0xE0 b = b & 0xE0 x0 = (width // 16) * (i % 16) y0 = (height // 32) * (i // 16) for x in range(x0, x0 + (width // 16)): for y in range(y0, y0 + (height // 32)): rgb[y, x] = [r, g, b] r = (r >> 5) << 6 g = (g >> 5) << 3 b = (b >> 5) << 0 print("%s" % format(r | g | b, "09b")) f.close() # Create an image from the RGB array image = Image.fromarray(rgb, 'RGB') # Save the image in BMP format image.save('out.bmp')