5 ms·
Okmain: How to pick an OK main colour of an image
- latexr 7mo agoI’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 7mo agolooks like it's a rust lib with a python wrapper. making a CLI tool should be just a few lines of code.
- deleted 7mo ago[deleted]
- latexr 7mo agoYeah, 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.
- blipvert 7mo agoThis sounds like a job for <ta-ta-ta-taaaa> contrib-directory-man!
- latexr 7mo agoSo your solution to “I’d be interested in having a small ready-made tool and try this out” is “spend a bunch of time to get acquainted with the code base of something you may not even like, create a separate tool, and submit it without even knowing if it’ll be accepted”? That’s like having someone looking at a display of ice cream in a supermarket saying “I’d be interested in trying a few samples before committing” and then getting a reply like “here are the recipes for all the ice creams, you can try to make them at home and taste them for yourself”. I know I could theoretically spend my weekend working on a CLI tool for this or making ice cream. Every developer knows that, there’s no reason to point that out except snark. But you know who might do it even faster and better and perhaps even enjoy it? The author. Look, the maintainer owes me nothing. I owe them nothing. This project has been shared to HN by the author and I’m making a simple, sensible, and sensical suggestion for something which I would like to see and believe would be an improvement overall, and I explained why. The author is free to agree or disagree, reply or ignore. Every one of those options is fine.
- cvwright 7mo agoYou’re not wrong, but you probably could have built the thing with Claude in the time it took you to write this comment.
- blipvert 7mo agoYou could likely have written that tool in the time that it took to write that comment. If it would be useful to you, and to others, then why not? And if it is not accepted then publish it yourself. Stop your whining.
- latexr 7mo agoI have explained why up the thread. I gave three reasons. Why are you feeling the need to be unnecessarily combative to a stranger on the internet? The author already said they think it’s a good idea and that they’ll work on it over the weekend, you’re being rude over nothing.
- blipvert 7mo agoSomeone gave you a constructive suggestion. > (which I don’t enjoy) > (which I avoid) > (which I don’t want) You are quite the negative Nelly.
- blipvert 7mo ago> The author already said they think it’s a good idea and that they’ll work on it over the weekend Did you even say “thank you”?
- latexr 7mo agoAm I talking to the secret HN account of JD Vance right now? I upvoted. Comments saying just thank you aren’t the norm for HN. No need to furiously send multiple replies to the same comment, either. There’s a link to the guidelines at the bottom if you need a refresher.
- sorenjan 7mo ago> uvx --with pillow --with okmain python -c "from PIL import Image; import okmain; print(okmain.colors(Image.open('bluemarble.jpg')))" [RGB(r=79, g=87, b=120), RGB(r=27, g=33, b=66), RGB(r=152, g=155, b=175), RGB(r=0, g=0, b=0)] It would make sense to add an entrypoint in the pyproject.toml so you can use uvx okmain directly.
- jcynix 7mo agoImageMagick is a wonderful command line tool, IMO. You could use it to extract various information, e.g. the 5 most used colors of an image, as in convert $IMG -colors 5 -depth 8 -format "%c" histogram:info: | sort -nr If needed you can easily remove colored borders first (trim subcommand with fuzz option) or sample only xy% from the image's center, or where the main subject might be.
- dgroshev 7mo agoGood idea, I'll add a CLI tool over the weekend.
- iamcalledrob 7mo agoAs 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! As a defensive step, I'd add something like this https://github.com/iamcalledrob/saferimg/blob/master/asset/pngbomb.png https://github.com/iamcalledrob/saferimg/blob/master/asset/p... to your test suite and see what happens.
- latexr 7mo 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.
- PaulHoule 7mo agoBack in the late 1980s people thought about color quantization a lot because a lot of computers of the time had 16 or 256 colors you could choose out of a larger palette and if you chose well you could do pretty well with photographic images.
- dylan604 7mo agoFred's dominantcolor script for imagemagick might work for you: https://www.fmwconcepts.com/imagemagick/dominantcolor/index.php https://www.fmwconcepts.com/imagemagick/dominantcolor/index....
- llimllib 7mo agoOKPalette 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/ https://okpalette.color.pizza/
- lemonad 7mo agoThis is nice! I looked into this quite a lot some years back when I was trying to summarize IKEA catalogs using color and eventually wrote an R package if you want to look into an alternative to e.g. k-means: https://github.com/lemonad/colorhull https://github.com/lemonad/colorhull (download https://github.com/lemonad/ikea-colors-through-time/blob/master/Report/report.html https://github.com/lemonad/ikea-colors-through-time/blob/mas... for more details on how it works)
- deleted 7mo ago[deleted]
- bee_rider 7mo agoI’m surprised the baseline to compare against is shrinking the image to one pixel, that seems extremely hacky and very dependent on what your image editor happens to do (and also seems quite wasteful… the rescaling operation must be doing a lot of extra pointless work keeping track of the position of pixels that are all ultimately going to be collapsed to one point). So, making a library that provides an alternative is a great service to the world, haha. An additional feature that might be nice: the most prominent colors seem like they might be a bad pick in some cases, if you want the important part of the image to stand out. Maybe a color that is the close (in the color space) to the edges of your image, but far away (in the color space) from the center of your image could be interesting?
- mungoman2 7mo agoTbh shrinking the image is probably the cheapest operation you can do that still lets every pixel influence the result. It’s just the average of all pixels, after suitable color conversion.
- bombcar 7mo agoIt might work decently well, but I wonder if it makes it "visually" match - sometimes the perfect average is not what our eyes see as the color.
- LoganDark 7mo agoThe author of the article seems to assume there is no color conversion (e.g., the resizing of the image is done with sRGB-encoded values rather than converting them to linear first). Which is a stupid way to do it but I'd believe most handwritten routines are just that.
- useftmly 7mo ago[flagged]
- dgroshev 7mo agoPicking the best colour is a difficult problem, I don't think there's a good general answer short of ML magic. I assembled an adversarial collection of images: https://github.com/si14/okmain/blob/main/test_images/IMG_1347.jpeg https://github.com/si14/okmain/blob/main/test_images/IMG_134... https://github.com/si14/okmain/blob/main/test_images/pendant_unicorn.jpg https://github.com/si14/okmain/blob/main/test_images/pendant... https://github.com/si14/okmain/blob/main/test_images/pendant_virgin.jpg https://github.com/si14/okmain/blob/main/test_images/pendant... https://github.com/si14/okmain/blob/main/test_images/red_moon.jpg https://github.com/si14/okmain/blob/main/test_images/red_moo... https://github.com/si14/okmain/blob/main/test_images/supremus_55.jpg https://github.com/si14/okmain/blob/main/test_images/supremu... For every heuristic, I can think of an image that breaks it. On the other hand, I just wanted to do better than the 1x1 trick, and I think the library clears that bar.
- useftmly 7mo ago[flagged]
- airstrike 7mo agoSee also https://github.com/material-foundation/material-color-utilities/blob/main/dev_guide%2Fextracting_colors.md https://github.com/material-foundation/material-color-utilit...
- eloisius 7mo agoReally interesting read. Thanks for sharing. Is the performance bottleneck around the resizing to 250k pixels? Would it still work if you sampled 15,625 4x4 patches evenly around the image to gather those pixels instead of resizing?
- bawolff 7mo agoIn the past when i tried just using image magick's built in -kmeans for this, i found chosing the second most prominent colour often looked really good. The primary was too much of the same thing.
- vova_hn2 7mo ago> simple 1x1 resize How is it "simple"? There are like a ton of different downscaling algorithms and each of them might produce a different result. Cool article otherwise.
- gzread 7mo agoAt 1x1 I don't expect any difference. It would be the average of all pixels in the image if you don't unevenly weight them (which you might decide when choosing a main color, but no downscaling algorithm would do) and the only difference is whether you remembered to gamma-correct.
- vova_hn2 7mo agoNearest-neighbor interpolation may pick just one pixel closest to the center.
- kristjan 7mo agoI've been doing something similar! I've got a Home Assistant dashboard on my desk and wanted the media controls to match the current album art. I need three colors: background, foreground, and something vibrant to set my desk lamp to [1]. The SpotifyPlus HA integration [2] was near at hand and does a reasonably good job clustering with a version of ColorThief [3] under the hood. It has the same two problems you started with though: muddying when there's lots of gradation, even within a cluster; and no semantic understanding when the cover has something resembling a frame. A bit swapped from okmain's goal, but I can invert with the best of them and will give it a shot next time I fiddle. Thanks for posting! [1] https://gist.github.com/kristjan/b305b83b0eb4455ee8455be108a0f703 https://gist.github.com/kristjan/b305b83b0eb4455ee8455be108a... [2] https://github.com/thlucas1/homeassistantcomponent_spotifyplus https://github.com/thlucas1/homeassistantcomponent_spotifypl... [3] https://github.com/thlucas1/SpotifyWebApiPython/blob/master/spotifywebapipython/vibrant/colorthieffast.py https://github.com/thlucas1/SpotifyWebApiPython/blob/master/...
- slazaro 7mo agoIt reminds me a bit of this post from the Facebook engineering blog (2015) [1] where they discuss embedding a very tiny preview of images into the html itself so they show immediately while loading the page, especially with very slow connections. [1] https://engineering.fb.com/2015/08/06/android/the-technology-behind-preview-photos/ https://engineering.fb.com/2015/08/06/android/the-technology...
- GauntletWizard 7mo agoI really like this approach. I worked on this problem (create a nice background for an image) for a couple weeks many years ago while organizing my desktop wallpaper collection, and never came up with a good answer. Unfortunately, I think that it's been "solved" in the tiktok era; an enlarged and blurred version of the image is used to fill the background space. The blurred mirror is inoffensive to almost everyone, and yet it always strikes me as gauche. Easy to ignore and yet I feel that it adds a lot of useless visual noise.
- Animats 7mo agoI've wanted something like this for level of detail processing. This is a render from Second Life, in which all the texture images were shrunk down to one pixel, the lowest possible level of detail, producing a monocolor image. For distant objects, or for objects where the texture is still coming in from the net, there needs to be some default color. The existing system used grey for everything. I tried using an average of all the pixels, and, as the original poster points out, the result looks murky.[1] This new approach has real promise for big-world rendering. [1] https://media.invisioncic.com/Mseclife/monthly_2023_05/monocolor1.jpg.002a5e610b7ac93e282a7e916dc46c38.jpg https://media.invisioncic.com/Mseclife/monthly_2023_05/monoc...
- Groxx 7mo agoI thought somewhere online there was a relatively-old (Material v1?) breakdown of how Android selects "main" colors (because it does a fairly good job imo), for the Palette¹ class... but I'm having no luck finding it at all. I can (and will) just read its source code, but it doesn't carry along any qualitative justification or comparisons or etc and it'll take a fair bit of time to re-research that knowledge. Does anyone know how this strategy differs? I've been wanting to build a product-color-selection-thing for stuff like fabric, where finding something that has similar tones is important, but I'm struggling to find much with concrete details about the strategy like this article has. 1: https://developer.android.com/reference/androidx/palette/graphics/Palette https://developer.android.com/reference/androidx/palette/gra... with most of the color selection logic here: https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:palette/palette/src/main/java/androidx/palette/graphics/ColorCutQuantizer.java https://cs.android.com/androidx/platform/frameworks/support/...