12345678910111213141516171819202122232425262728293031323334353637 |
- #!/usr/bin/env python3
- from PIL import Image
- def ingest_png(file_name: str) -> tuple[str, int, int, int, int]:
- print(f"Ingesting {file_name}")
- # 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]
- # 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)
- return name, x, yr_sum * yfactor, yg_sum * yfactor, yb_sum * yfactor
- 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)
|