|
@@ -1,34 +1,30 @@
|
|
|
-from re import M
|
|
|
+#!/usr/bin/env python3
|
|
|
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 ingest_png(file_name: str) -> tuple[str, int, int, int, int]:
|
|
|
+ print(f"Ingesting {file_name}")
|
|
|
|
|
|
-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)
|
|
|
+ # image name - strip leading path and trailing extension
|
|
|
+ name = file_name.rsplit("/", maxsplit=1)[1].split(".", maxsplit=1)[0]
|
|
|
|
|
|
+ # read non-transparent pixels of image as RGB values
|
|
|
+ pixels = [(r, g, b) for r, g, b, a in Image.open(file_name).convert("RGBA").getdata() if a > 0]
|
|
|
|
|
|
-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]
|
|
|
+ # X metric - mean sq norm of all pixels
|
|
|
+ x = sum(x * x + y * y + z * z for x, y, z in pixels) / len(pixels)
|
|
|
|
|
|
+ # Y metrics - 2 * mean of color components
|
|
|
+ yr_sum = 0
|
|
|
+ yg_sum = 0
|
|
|
+ yb_sum = 0
|
|
|
+ for r, g, b in pixels:
|
|
|
+ yr_sum += r
|
|
|
+ yg_sum += g
|
|
|
+ yb_sum += b
|
|
|
+ yfactor = 2 / len(pixels)
|
|
|
|
|
|
-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
|
|
|
+ return name, x, yr_sum * yfactor, yg_sum * yfactor, yb_sum * yfactor
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|