Skip to content

Colorpalette from image: nice to have #84

@lazarusA

Description

@lazarusA

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions