by dgroshev · 8mo ago · 0 comments
As for loading into memory at once: I suppose I could integrate with something like libvips and stream strips out of the decoded image without holding the entire bitmap, but that'd require substantially more glue and complexity. The current approach works fine for extracting dominant colours once to save in a database.
You're right that pre-resizing the images makes everything faster, but keep in mind that k-means still requires a pretty nontrivial amount of computation.
image = pyvips.Image.new_from_file(filename, access="sequential")
scale = min(200 / image.width, 200 / image.height)
thumbnail = image.colourspace("oklab").resize(scale).extract_bands(0, n=3)
rgbf = thumbnail.write_to_memory()
That'll stream the source image and make a RGBRGBRGB memory buffer of single precision floats. You could perhaps use kernel="linear" and avoid any ringing from lanczos3.
I think I would downsample in a linear light space, like scRGB. Averaging there means averaging photons, which will surely be better than OKLab. Maybe switch to OKLab for clustering. Though of course I've not tested it.