Moved ProotState to own file.

Moved Point2D to own file.
Added caching for parsed images.
This commit is contained in:
CiscoTheWolf 2023-05-29 20:09:29 +02:00
parent bdcb648765
commit ab34ad2455
6 changed files with 340 additions and 259 deletions

View file

@ -2,50 +2,97 @@ import math
from PIL import Image
import numpy as np
from scipy.optimize import linear_sum_assignment
import json
import os.path
import hashlib
# location to store array cache
CACHE_FILE_PATH = "point_array_cache.json"
class Point2D:
x = 0
y = 0
def __init__(self, x, y):
color: tuple[int, int, int] = (0, 0, 0)
def __init__(self, x, y, color: tuple[int, int, int] = (0, 0, 0)):
self.x = x
self.y = y
self.color = color
def round(self):
self.x = round(self.x)
self.y = round(self.y)
return self
def distance(self, other):
dx = self.x - other.x
dy = self.y - other.y
return math.sqrt(dx**2 + dy**2)
return math.sqrt(dx ** 2 + dy ** 2)
def interpolate(self, other, percentage):
new_x = self.x + (other.x - self.x) * percentage
new_y = self.y + (other.y - self.y) * percentage
return Point2D(new_x, new_y)
new_color = tuple(int((1 - percentage) * self.color[i] + percentage * other.color[i]) for i in range(3))
return Point2D(new_x, new_y, new_color)
def __eq__(self, other):
return (self.x, self.y) == (other.x, other.y)
def mirror_points(points: list[Point2D]) -> list[Point2D]:
mirrored_points = []
for point in points:
mirrored_x = 128 - point.x # Calculate the mirrored x-coordinate
mirrored_point = Point2D(mirrored_x, point.y)
mirrored_points.append(mirrored_point)
return mirrored_points
def get_image_hash(image):
image_hash = hashlib.sha1(image.tobytes()).hexdigest()
return image_hash
def load_cached_point_arrays():
cached_point_arrays = {}
if os.path.isfile(CACHE_FILE_PATH):
with open(CACHE_FILE_PATH, "r") as file:
cached_point_arrays = json.load(file)
return cached_point_arrays
def save_cached_point_arrays(cached_point_arrays):
with open(CACHE_FILE_PATH, "w") as file:
json.dump(cached_point_arrays, file)
def generate_point_array_from_image(image):
image = image.convert("RGB") # Convert image to RGB color mode
image_hash = get_image_hash(image)
cached_point_arrays = load_cached_point_arrays()
if image_hash in cached_point_arrays:
return [Point2D(point["x"], point["y"], tuple(point["color"])) for point in cached_point_arrays[image_hash]]
width, height = image.size
point_array = []
pixel_array = []
# Iterate over the pixels and generate Point2D instances
for y in range(height):
for x in range(width):
pixel = image.getpixel((x, y))
if pixel != (0, 0, 0): # Assuming white pixels
point = Point2D(x, y)
point_array.append(point)
if pixel != (0, 0, 0): # any non-white pixels
point = {"x": x, "y": y, "color": pixel}
pixel_array.append(point)
return point_array
cached_point_arrays[image_hash] = pixel_array
save_cached_point_arrays(cached_point_arrays)
return [Point2D(point["x"], point["y"], tuple(point["color"])) for point in pixel_array]
def generate_image_from_point_array(points, width, height):
@ -114,8 +161,8 @@ def interpolate_point_pairs(pairs, percentage):
return interpolated_points
Image1 = Image.open("CiscoTheProot/faces/prootface3.png")
Image2 = Image.open("CiscoTheProot/faces/prootface4.png")
Image1 = Image.open("faces/prootface3.png")
Image2 = Image.open("faces/prootface4.png")
pixelArray1 = generate_point_array_from_image(Image1)
pixelArray2 = generate_point_array_from_image(Image2)