Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/python3
# Interpolate depth maps
from PIL import Image
import math
import random
contours = Image.open("contours.png")
contourspx = contours.load()
w, h = contours.size
def nearest_contour(x0, y0, angle):
dx = math.cos(angle)
dy = math.sin(angle)
for i in range(300):
x = x0 + dx * i
y = y0 + dy * i
if x < 0 or y < 0 or x >= w or y >= h: return None, None
px = contourspx[x, y]
if px != 255:
return px, i
return None, None
result = contours.copy()
resultpx = result.load()
for y in range(0, h):
print("%d/%d" % (y, h))
if y % 10 == 0: result.save("result.png")
for x in range(0, w):
if contourspx[x, y] == 255:
sum_vals = 0.0
sum_weights = 0.0
for angle in range(0, 360, 5):
rad = angle * math.pi / 180
c, d = nearest_contour(x, y, rad)
if c is not None:
weight = 1.0 / d**0.9
sum_vals += c * weight
sum_weights += weight
if sum_weights == 0:
v = (resultpx[x - 1, y] + resultpx[x, y - 1]) / 2
else:
v = sum_vals / sum_weights
resultpx[x, y] = int(round(v + random.normalvariate(0.0, 1.0)))
result.save("result.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment