from PIL import Image from PIL import ImagePalette palettedata = [] for v in range(32): r = ((v>>3)&3)*0x55 g = ((v>>1)&3)*0x55 b = (v&1)*0xff palettedata.extend((r,g,b)) for v in range(32): r = ((v>>3)&3)*0x55 g = ((v>>1)&3)*0x55 b = 0x80+(v&1)*0x7f palettedata.extend((r,g,b)) palettedata.extend([0] * (768-len(palettedata))) palimage = Image.new("P", (1,1)) palimage.putpalette(palettedata) im = Image.open("finch.jpg") im2 = im.resize((40,480)).quantize("P", palette=palimage) im2.save("finch3.png") pal = im2.getpalette() im3 = Image.new("RGB", (16*im2.width,im2.height)) for y in range(im2.height): for x in range(im2.width): v = im2.getpixel((x,y)) & 31 r,g,b = pal[3*v:3*(v+1)] for xx in range(16): im3.putpixel((x*16+xx,y),(r,g,b)) im3.save("finch4.png") with open("finch.img", "wb") as f: f.write(im2.tobytes()) f.close()