-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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