#!python # -*- mode: python; Encoding: utf-8; coding: utf-8 -*- # Last updated: <2022/07/20 09:16:10 +0900> """ Create sweetie-16 .gpl (GIMP palette file) and png image. Windows10 x64 21H2 + Python 3.9.13 64bit + Pillow 9.1.1 """ from PIL import Image from PIL import ImageDraw out_gpl_file = "sweetie-16_tic80new_.gpl" out_img_file = "sweetie-16_tic80new.png" title = "Sweetie-16 (TIC-80 new)" tbl = [ ["Black", 0, "1A1C2C"], ["Purple", 1, "5D275D"], ["Red", 2, "B13E53"], ["Orange", 3, "EF7D57"], ["Yellow", 4, "FFCD75"], ["Light Green", 5, "A7F070"], ["Green", 6, "38B764"], ["Dark Green", 7, "257179"], ["Dark Blue", 8, "29366F"], ["Blue", 9, "3B5DC9"], ["Light Blue", 10, "41A6F6"], ["Cyan", 11, "73EFF7"], ["White", 12, "F4F4F4"], ["Light Grey", 13, "94B0C2"], ["Grey", 14, "566C86"], ["Dark Grey", 15, "333C57"], ] def main(): pal = [] # create .gpl lst = [] lst.append("GIMP Palette") lst.append("Name: %s" % title) lst.append("Columns: 8") lst.append("#") lst.append("#") for t in tbl: name, index, hexstr = t col = int(hexstr, 16) r = (col >> 16) & 0x0ff g = (col >> 8) & 0x0ff b = col & 0x0ff s = "%-8d%-8d%-8d%06x %d %s" % (r, g, b, col, index, name) lst.append(s) pal.append((r, g, b)) for s in lst: print(s) # save .gpl file with open(out_gpl_file, mode="w") as f: f.write("\n".join(lst)) f.write("\n") print("# Save %s" % out_gpl_file) # create image and save xcnt, ycnt = 8, 2 # xcnt, ycnt = 16, 1 w, h = 32, 32 img_w, img_h = w * xcnt, h * ycnt im = Image.new("P", (img_w, img_h), (0, 0, 0)) paldata = [] for c in pal: r, g, b = c paldata.append(r) paldata.append(g) paldata.append(b) im.putpalette(paldata, rawmode="RGB") draw = ImageDraw.Draw(im) x, y = 0, 0 for c in range(len(pal)): draw.rectangle((x, y, x + w - 1, y + h - 1), fill=c) x += w if x >= img_w: x = 0 y += h im.save(out_img_file) print("# Save %s" % out_img_file) if __name__ == '__main__': main()