|
@@ -0,0 +1,41 @@
|
|
|
+from re import M
|
|
|
+from PIL import Image
|
|
|
+
|
|
|
+
|
|
|
+def calc_X_metric(pixels: list[tuple[int, int, int]]) -> float:
|
|
|
+ return sum(x * x + y * y + z * z for x, y, z in pixels) / len(pixels)
|
|
|
+
|
|
|
+
|
|
|
+def calc_Y_metric(pixels: list[tuple[int, int, int]]) -> tuple[float, float, float]:
|
|
|
+ r_sum = 0
|
|
|
+ g_sum = 0
|
|
|
+ b_sum = 0
|
|
|
+ for r, g, b in pixels:
|
|
|
+ r_sum += r
|
|
|
+ g_sum += g
|
|
|
+ b_sum += b
|
|
|
+ factor = 2 / len(pixels)
|
|
|
+ return (r_sum * factor, g_sum * factor, b_sum * factor)
|
|
|
+
|
|
|
+
|
|
|
+def read_png_data(img_name: str) -> list[tuple[int, int, int]]:
|
|
|
+ return [(r, g, b) for r, g, b, a in Image.open(img_name).convert("RGBA").getdata() if a > 0]
|
|
|
+
|
|
|
+
|
|
|
+def ingest_png(file_name: str):
|
|
|
+ print(f"Ingesting {file_name}")
|
|
|
+ data = read_png_data(file_name)
|
|
|
+ x = calc_X_metric(data)
|
|
|
+ y = calc_Y_metric(data)
|
|
|
+ name = file_name.rsplit("/", maxsplit=1)[1].split(".", maxsplit=1)[0]
|
|
|
+ return name, x, *y
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ import csv
|
|
|
+ import os
|
|
|
+
|
|
|
+ data = [ingest_png("pngs/" + fn) for f in os.listdir("pngs") if (fn := os.fsdecode(f)).endswith(".png")]
|
|
|
+ with open("database.csv", "w") as outfile:
|
|
|
+ writer = csv.writer(outfile, delimiter=",", quotechar="'")
|
|
|
+ writer.writerows(data)
|