|
@@ -1,9 +1,9 @@
|
|
|
#!/usr/bin/env python3
|
|
|
from collections import namedtuple
|
|
|
|
|
|
+import numpy as np
|
|
|
from PIL import Image
|
|
|
-
|
|
|
-from convert import rgb_to_cieluv
|
|
|
+from colorspacious import cspace_convert
|
|
|
|
|
|
|
|
|
def is_outline(r: int, g: int, b: int, a: int) -> bool:
|
|
@@ -11,20 +11,20 @@ def is_outline(r: int, g: int, b: int, a: int) -> bool:
|
|
|
return a == 0 or (r, g, b) == (0, 0, 0)
|
|
|
|
|
|
|
|
|
-def x_metric(pixels: list[tuple[float, float, float]]) -> float:
|
|
|
+def x_metric(pixels: np.array) -> float:
|
|
|
# X metric - the mean squared Euclidean norm
|
|
|
# computed as the sum of the squares of the components of the pixels,
|
|
|
# normalized by the number of pixels
|
|
|
- return sum(comp * comp for pix in pixels for comp in pix) / len(pixels)
|
|
|
+ return sum(sum(pixels ** 2)) / len(pixels)
|
|
|
|
|
|
|
|
|
-def y_metric(pixels: list[tuple[float, float, float]]) -> tuple[float, float, float]:
|
|
|
+def y_metric(pixels: np.array) -> np.array:
|
|
|
# Y metric - the mean pixel of the image
|
|
|
- return tuple(sum(p[i] for p in pixels) / len(pixels) for i in range(3))
|
|
|
+ return sum(pixels) / len(pixels)
|
|
|
|
|
|
|
|
|
ImageInfo = namedtuple(
|
|
|
- "ImageInfo", ["name", "xrgb", "xluv", "yr", "yg", "yb", "yl", "yu", "yv"]
|
|
|
+ "ImageInfo", ["name", "xrgb", "xcam", "yrgb", "ycam"]
|
|
|
)
|
|
|
|
|
|
|
|
@@ -35,30 +35,22 @@ def ingest_png(file_name: str) -> ImageInfo:
|
|
|
name = file_name.rsplit("/", maxsplit=1)[1].split(".", maxsplit=1)[0]
|
|
|
|
|
|
# read non-outline pixels of image
|
|
|
- rgb_pixels = [
|
|
|
+ rgb_pixels = np.array([
|
|
|
(r, g, b)
|
|
|
for r, g, b, a in Image.open(file_name).convert("RGBA").getdata()
|
|
|
if not is_outline(r, g, b, a)
|
|
|
- ]
|
|
|
+ ])
|
|
|
|
|
|
- # convert RGB pixels to CIELUV values
|
|
|
- luv_pixels = [rgb_to_cieluv(*p) for p in rgb_pixels]
|
|
|
+ # convert RGB pixels to CAM02 values
|
|
|
+ cam_pixels = cspace_convert(rgb_pixels, "sRGB255", "CAM02-UCS")
|
|
|
|
|
|
# compute and return metrics
|
|
|
- xrgb = x_metric(rgb_pixels)
|
|
|
- xluv = x_metric(luv_pixels)
|
|
|
- yr, yg, yb = y_metric(rgb_pixels)
|
|
|
- yl, yu, yv = y_metric(luv_pixels)
|
|
|
return ImageInfo(
|
|
|
name=name,
|
|
|
- xrgb=xrgb,
|
|
|
- xluv=xluv,
|
|
|
- yr=yr,
|
|
|
- yg=yg,
|
|
|
- yb=yb,
|
|
|
- yl=yl,
|
|
|
- yu=yu,
|
|
|
- yv=yv,
|
|
|
+ xrgb=x_metric(rgb_pixels),
|
|
|
+ xcam=x_metric(cam_pixels),
|
|
|
+ yrgb=y_metric(rgb_pixels),
|
|
|
+ ycam=y_metric(cam_pixels),
|
|
|
)
|
|
|
|
|
|
|
|
@@ -77,11 +69,11 @@ if __name__ == "__main__":
|
|
|
|
|
|
with open("database.csv", "w") as outfile:
|
|
|
writer = csv.writer(outfile, delimiter=",", quotechar="'")
|
|
|
- writer.writerows([d.name, d.xrgb, d.yr, d.yg, d.yb] for d in data)
|
|
|
+ writer.writerows([d.name, d.xrgb, *d.yrgb] for d in data)
|
|
|
|
|
|
- with open("database-luv.csv", "w") as outfile:
|
|
|
+ with open("database-cam02.csv", "w") as outfile:
|
|
|
writer = csv.writer(outfile, delimiter=",", quotechar="'")
|
|
|
- writer.writerows([d.name, d.xluv, d.yl, d.yu, d.yv] for d in data)
|
|
|
+ writer.writerows([d.name, d.xcam, *d.ycam] for d in data)
|
|
|
|
|
|
with open("database.js", "w") as outfile:
|
|
|
outfile.write("const database = [\n")
|
|
@@ -90,9 +82,9 @@ if __name__ == "__main__":
|
|
|
(
|
|
|
f'name: "{info.name}"',
|
|
|
f"xRGB: {info.xrgb}",
|
|
|
- f"xLUV: {info.xluv}",
|
|
|
- f"yRGB: [ {info.yr}, {info.yg}, {info.yb} ]",
|
|
|
- f"yLUV: [ {info.yl}, {info.yu}, {info.yv} ]",
|
|
|
+ f"xCAM02: {info.xcam}",
|
|
|
+ f"yRGB: [ {', '.join(str(c) for c in info.yrgb)} ]",
|
|
|
+ f"yCAM02: [ {', '.join(str(c) for c in info.ycam)} ]",
|
|
|
)
|
|
|
)
|
|
|
outfile.write(f" {{ {fields} }},\n")
|