with open("rgb332.gpl", "w") as f: f.write("GIMP Palette\nName: RGB332\nColumns: 16\n#\n") for i in range(256): r = ((i >> 5) * 255) // 7 g = (((i >> 2) & 0x7) * 255) // 7 b = ((i & 0x3) * 255) // 3 f.write(f"{r:3d} {g:3d} {b:3d} Index {i}\n")