Skip to content

Instantly share code, notes, and snippets.

@tomconte
Created September 16, 2012 18:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomconte/3733644 to your computer and use it in GitHub Desktop.
Save tomconte/3733644 to your computer and use it in GitHub Desktop.
Generate a Color Look Up Table (CLUT) for the Commodore 64 palette, using the Python Image Library (PIL)
#!/usr/local/bin/python
import math
import Image
# C64 color palette
COLOR_BLACK = (0, 0, 0)
COLOR_WHITE = (255, 255, 255)
COLOR_RED = (104, 55, 43)
COLOR_CYAN = (112, 164, 178)
COLOR_PURPLE = (111, 61, 134)
COLOR_GREEN = (88, 141, 67)
COLOR_BLUE = (53, 40, 121)
COLOR_YELLOW = (184, 199, 111)
COLOR_ORANGE = (111, 79, 37)
COLOR_BROWN = (67, 57, 0)
COLOR_LIGHTRED = (154, 103, 89)
COLOR_DARKGREY = (68, 68, 68)
COLOR_GREY = (108, 108, 108)
COLOR_LIGHTGREEN = (154, 210, 132)
COLOR_LIGHTBLUE = (108, 94, 181)
COLOR_LIGHTGREY = (149, 149, 149)
palette = [ COLOR_BLACK, COLOR_WHITE, COLOR_RED, COLOR_CYAN, COLOR_PURPLE, COLOR_GREEN, COLOR_BLUE, COLOR_YELLOW, COLOR_ORANGE, COLOR_BROWN, COLOR_LIGHTRED, COLOR_DARKGREY, COLOR_GREY, COLOR_LIGHTGREEN, COLOR_LIGHTBLUE, COLOR_LIGHTGREY ]
im = Image.open("lookup.png")
width, height = im.size
pix = im.load()
destim1 = Image.new('RGB', (width, height))
dest1 = destim1.load()
destim2 = Image.new('RGB', (width, height))
dest2 = destim2.load()
def calc_dist(p1,p2):
return math.sqrt((p2[0] - p1[0]) ** 2 +
(p2[1] - p1[1]) ** 2 +
(p2[2] - p1[2]) ** 2)
for y in xrange(height):
for x in xrange(width):
distances = []
for c in palette:
distances.append(calc_dist(pix[x,y], c))
sortdist = sorted(distances)
dest1[x, y] = palette[distances.index(sortdist[0])]
dest2[x, y] = palette[distances.index(sortdist[1])]
destim1.save("paletteC64_first.png")
destim2.save("paletteC64_second.png")
@usahg
Copy link

usahg commented Jun 3, 2015

I'm looking for a way to convert 3DL or LUT files to png color map look up tables.
And I for the life of me can't find that on the internet. Would really appreciate your help.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment