Skip to content
{{ message }}

Instantly share code, notes, and snippets.

# PetteriAimonen/interpolate.py

Last active Dec 31, 2020
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
 #!/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")
to join this conversation on GitHub. Already have an account? Sign in to comment