As a designer, I've built variants of this several times throughout my career.
The author's approach is really good, and he hits on pretty much all the problems that arise from more naive approaches. In particular, using a perceptual colorspace, and how the most representative colour may not be the one that appears the most.
However, image processing makes my neck tingle because there are a lot of footguns. PNG bombs, anyone? I feel like any library needs to either be defensively programmed or explicit in its documentation.
The README says "Finding main colors of a reasonably sized image takes about 100ms" -- that's way too slow. I bet the operation takes a few hundred MB of RAM too.
For anyone that uses this, scale down your images substantially first, or only sample every N pixels. Avoid loading the whole thing into memory if possible, unless this handled serially by a job queue of some sort.
You can operate this kind of algorithm much faster and with less RAM usage on a small thumbnail than you would on a large input image. This makes performance concerns less of an issue. And prevents a whole class of OOM DoS vulnerabilities!
I really wish people would read the article, the library does exactly this:
> Okmain downsamples the image by a power of two until the total number of pixels is below 250,000.
latexr 49 minutes ago [-]
> I've built variants of this several times throughout my career.
Got any to share? A self-contained command-line tool to get a good palette from an image is something I’d have a use for.
llimllib 44 minutes ago [-]
OKPalette by David Aerne is my favorite tool for this, it chooses points sensibly but then also lets you drag around or change the number of colors you want: https://okpalette.color.pizza/
Nice article! Just curious, do people intuitively understand what a perceptually uniform color space is without much more explanation?
I feel you really need to understand first that typical color spaces like HSL used for color picker UIs have the very counterintuitive flaw that the lightness you see with your eye of a color changes when you only change the saturation or hue sliders. As in, it's very surprising the color spaces most designers use are not perceptually uniform.
latexr 2 hours ago [-]
I’d be interested in trying this out as a command-line tool. It would be useful on its own and the fastest way to evaluate results.
woodrowbarlow 1 hours ago [-]
looks like it's a rust lib with a python wrapper. making a CLI tool should be just a few lines of code.
latexr 8 minutes ago [-]
Yeah, but then I’d have to be working with Python (which I don’t enjoy) and be pulling in dependencies (which I avoid) to have a custom system with moving parts (Python interpreter, library, script) (which I don’t want).
A rust CLI would make a lot of sense here. Single binary.
58 minutes ago [-]
Rendered at 13:28:12 GMT+0000 (Coordinated Universal Time) with Vercel.
The author's approach is really good, and he hits on pretty much all the problems that arise from more naive approaches. In particular, using a perceptual colorspace, and how the most representative colour may not be the one that appears the most.
However, image processing makes my neck tingle because there are a lot of footguns. PNG bombs, anyone? I feel like any library needs to either be defensively programmed or explicit in its documentation.
The README says "Finding main colors of a reasonably sized image takes about 100ms" -- that's way too slow. I bet the operation takes a few hundred MB of RAM too.
For anyone that uses this, scale down your images substantially first, or only sample every N pixels. Avoid loading the whole thing into memory if possible, unless this handled serially by a job queue of some sort.
You can operate this kind of algorithm much faster and with less RAM usage on a small thumbnail than you would on a large input image. This makes performance concerns less of an issue. And prevents a whole class of OOM DoS vulnerabilities!
As a defensive step, I'd add something like this https://github.com/iamcalledrob/saferimg/blob/master/asset/p... to your test suite and see what happens.
> Okmain downsamples the image by a power of two until the total number of pixels is below 250,000.
Got any to share? A self-contained command-line tool to get a good palette from an image is something I’d have a use for.
I feel you really need to understand first that typical color spaces like HSL used for color picker UIs have the very counterintuitive flaw that the lightness you see with your eye of a color changes when you only change the saturation or hue sliders. As in, it's very surprising the color spaces most designers use are not perceptually uniform.
A rust CLI would make a lot of sense here. Single binary.