Skip to content

Instantly share code, notes, and snippets.

@insom
Created January 5, 2021 02:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save insom/05e5b0ee3c3dbb2bab8b141238b8510b to your computer and use it in GitHub Desktop.
Save insom/05e5b0ee3c3dbb2bab8b141238b8510b to your computer and use it in GitHub Desktop.
Create an internal colour lookup table from a reference image, apply it to a source image and save the result.
'''
Create an internal colour lookup table from a reference image, apply it to a
source image and save the result.
Aaron Brady, 2020
'''
from PIL import Image
from math import floor
def create_lookup_table():
w = 256
values = [[0]*w, [0]*w, [0]*w]
im = Image.open('polaroid.png')
prefixes = [w*13, w*23, w*33]
for ii, prefix in enumerate(prefixes):
for i in range(256):
values[ii][i] = im.getdata()[prefix + i]
return values
def munge(im, lut):
w, h = im.size
for x in range(w):
for y in range(h):
p = im.getpixel((x, y))
red_values = lut[0][p[0]]
green_values = lut[1][p[1]]
blue_values = lut[2][p[2]]
q = [0, 0, 0]
q[0] = floor(sum((red_values[0], green_values[0], blue_values[0])) / 3)
q[1] = floor(sum((red_values[1], green_values[1], blue_values[1])) / 3)
q[2] = floor(sum((red_values[2], green_values[2], blue_values[2])) / 3)
im.putpixel((x, y), tuple(q))
return im
lut = create_lookup_table()
im = Image.open('input.jpg')
im = munge(im, lut)
im.save('output.jpg')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment