Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colorpalette from image: nice to have #84

Open
lazarusA opened this issue Jan 12, 2022 · 2 comments
Open

Colorpalette from image: nice to have #84

lazarusA opened this issue Jan 12, 2022 · 2 comments

Comments

@lazarusA
Copy link

I do have this script to obtain colorpalettes from images (like the ones for Taylor Swift :D, https://twitter.com/drob/status/1459259255312236547 ). However, probably it will be nice to have a built-in function in ColorSchemes for this. Just in case this is of interest, here the relevant code (probably not the best approach, but it works).

using Clustering, Colors, Random
using Images: load, RGB, channelview, permutedims

function getColorPalette(image; ncolors = 5)
        img_CHWa = channelview(image)
        #img_CHW = permutedims(img_CHWa, (1,3,2))
        testmat = reshape(img_CHWa, (3, size(image)[1]*size(image)[2])) #input shape
        sol = kmeans(testmat, ncolors)
        csize = counts(sol) # get the cluster sizes
        colores = sol.centers # get the cluster centers, dominat colors
        indxc = sortperm(csize)
        colores, indxc
end
@cormullion
Copy link
Member

cormullion commented Jan 12, 2022

Hi Lazaro! Yes, that's a nice feature!

Some time ago, ColorSchemes.jl was split into two, prompted by the Plots folk: a barebones list of palettes (ColorSchemes.jl), and the various functions for building and modifying images - which eventually ended up in a separate repo, ColorSchemeTools.jl.

It would be interesting to compare the performance and results of your function and the one there : (https://github.com/JuliaGraphics/ColorSchemeTools.jl/blob/51b617c40ae03911e22bcb100b72a3e956e3844f/src/ColorSchemeTools.jl#L55).

I remember having to experiment with the options to get a palette which looks to my eyes a faithful representation of the image - I think clustering is a bit random?

I used to make views of movies with this function: here's the James Bond collection from a few years ago: https://imgur.com/a/AaZMV.

@lazarusA
Copy link
Author

lazarusA commented Jan 12, 2022

As a reference, here are some example of how the colours look like:
https://nextjournal.com/lazarus/extracting-dominant-colours-from-pictures

I will take a look at SchemeTools later (it's also clustering ). Ahh, is the same thing. Good to know that this already there.

Nice James Bond collections :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants