Skip to content

Instantly share code, notes, and snippets.

@FelixWolf
Created March 26, 2023 19:41
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 FelixWolf/9e0afdca148770307cd8164b5973f3e8 to your computer and use it in GitHub Desktop.
Save FelixWolf/9e0afdca148770307cd8164b5973f3e8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from PIL import Image
from PIL import ImageShow
ImageShow.register(ImageShow.DisplayViewer, 0)
def lerp(a, b, p):
return a + p * (b - a)
def bilinear(a, b, c, d, x, y):
return lerp(lerp(a, b, x), lerp(c, d, x), y)
def lerpColor(a, b, p):
return (
round(lerp(a[0], b[0], p)),
round(lerp(a[1], b[1], p)),
round(lerp(a[2], b[2], p))
)
def bilinearColor(a, b, c, d, x, y):
return lerpColor(lerpColor(a, b, x), lerpColor(c, d, x), y)
def colorDiff(a, b):
return (
b[0] - a[0],
b[1] - a[1],
b[2] - a[2]
)
def applyColorDiff(dest, diff):
return (
min(max(dest[0] + diff[0], 0), 255),
min(max(dest[1] + diff[1], 0), 255),
min(max(dest[2] + diff[2], 0), 255)
)
def loadLUT(im, size):
lut = [
[
[
None for _ in range(size)
] for _ in range(size)
] for _ in range(size)
]
px = im.load()
for z in range(0,size):
for y in range(0,size):
for x in range(0,size):
c = px[(z*size) + x, y]
lut[z][y][x] = c
return lut
def saveLUT(lut):
size = len(lut)
im = Image.new("RGB", (size*size, size))
px = im.load()
for z in range(0,size):
for y in range(0,size):
for x in range(0,size):
c = lut[z][y][x]
px[(z*size) + x, y] = c
return im
def lutUpscale(lut):
size = len(lut) * 2
newlut = [
[
[
(0,0,0) for _ in range(size)
] for _ in range(size)
] for _ in range(size)
]
for z in range(0,size,2):
for y in range(0,size,2):
for x in range(0,size,2):
newlut[z][y][x] = lut[z//2][y//2][x//2]
for z in range(0,size,2):
for y in range(0,size,2):
for x in range(1,size,2):
if x == size - 1:
test = colorDiff(lerpColor(
newlut[z][y][x - 3],
newlut[z][y][x - 1],
0.5
), newlut[z][y][x - 1])
newlut[z][y][x] = applyColorDiff(newlut[z][y][x-1], test)
else:
newlut[z][y][x] = lerpColor(
newlut[z][y][x - 1],
newlut[z][y][x + 1],
0.5
)
for z in range(0,size,2):
for y in range(1,size,2):
for x in range(0,size,2):
if y == size - 1:
test = colorDiff(lerpColor(
newlut[z][y - 3][x],
newlut[z][y - 1][x],
0.5
), newlut[z][y - 1][x])
newlut[z][y][x] = applyColorDiff(newlut[z][y-1][x], test)
else:
newlut[z][y][x] = lerpColor(
newlut[z][y - 1][x],
newlut[z][y + 1][x],
0.5
)
for z in range(0,size,2):
for y in range(1,size,2):
for x in range(1,size,2):
if y == size - 1 or x == size - 1:
test = colorDiff(bilinearColor(
newlut[z][y][x - 3],
newlut[z][y][x - 1],
newlut[z][y - 3][x],
newlut[z][y - 1][x],
0.5,
0.5
), newlut[z][y - 1][x])
newlut[z][y][x] = applyColorDiff(newlut[z][y-1][x-1], test)
else:
newlut[z][y][x] = bilinearColor(
newlut[z][y][x - 1],
newlut[z][y][x + 1],
newlut[z][y - 1][x],
newlut[z][y + 1][x],
0.5,
0.5
)
for z in range(1,size,2):
for y in range(0,size):
for x in range(0,size):
if z == size - 1:
test = colorDiff(lerpColor(
newlut[z - 3][y][x],
newlut[z - 1][y][x],
0.5
), newlut[z-1][y][x])
newlut[z][y][x] = applyColorDiff(newlut[z-1][y][x], test)
else:
newlut[z][y][x] = lerpColor(
newlut[z - 1][y][x],
newlut[z + 1][y][x],
0.5
)
return newlut
if __name__ == "__main__":
import glob
files = glob.glob("*.tga")
for file in files:
im = Image.open(file)
lut = loadLUT(im, 16)
lut = lutUpscale(lut)
im = saveLUT(lut)
im.save("resized/"+file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment