Created
November 25, 2023 01:40
-
-
Save Friz64/ef941181f4d6aef82332f7066f984815 to your computer and use it in GitHub Desktop.
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/python | |
import argparse | |
from PIL import Image | |
import numpy as np | |
import scipy | |
import matplotlib.pyplot as plt | |
START_X_OFFSET = 522 | |
GRADIENT_PIXELS = 122 | |
SCALE_FACTOR = 2 | |
def func(x, a, b, c): | |
return a * np.exp(-b * x) + c | |
parser = argparse.ArgumentParser() | |
parser.add_argument('image') | |
parser.add_argument('--plot', action='store_true') | |
args = parser.parse_args() | |
image = Image.open(args.image) | |
assert START_X_OFFSET + GRADIENT_PIXELS == image.width | |
pixels = image.load() | |
x_values = [] | |
y_values = [] | |
for i, xoff in enumerate(range(START_X_OFFSET, START_X_OFFSET + GRADIENT_PIXELS)): | |
r, g, b, a = pixels[xoff, image.height / 2] | |
x_values.append((i + 0.5) / SCALE_FACTOR) | |
y_values.append(a / 255) | |
x = np.array(x_values) | |
y = np.array(y_values) | |
popt, *_ = scipy.optimize.curve_fit(func, x, y) | |
shadow_size = 0 | |
while round(func(shadow_size, *popt) * 255) > 0: | |
shadow_size += 1 | |
print(f"a = {popt[0]}") | |
print(f"b = {popt[1]}") | |
print(f"c = {popt[2]}") | |
print(f"shadow size = {shadow_size}") | |
if args.plot: | |
plt.figure() | |
plt.plot(x, y, 'ko', label="Original Data") | |
plt.plot(x, func(x, *popt), 'r-', label="Fitted Curve") | |
plt.legend() | |
plt.title(args.image) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment