using Clustering, Images, ColorSchemes using Plots plotlyjs() function image2zvals(img::Union{Matrix{RGB{T}}, Matrix{RGBA{T}}}; n_colors=64, maxiter=200, tol=1e-04) where T<:Real rows, cols = size(img) observations = reshape(img, rows*cols) kmres = kmeans(observations, n_colors; maxiter = maxiter, tol=tol) nclust = nclusters(kmres) a = assignments(kmres)/nclust z_data = reshape(a, rows, cols) hcolors = kmres.centers # kmres.centers is the codebook if fieldcount(eltype(img)) == 3 cscheme = ColorScheme([RGB(c...) for c in eachcol(hcolors)]) else cscheme = ColorScheme([RGB(c[1:3]...) for c in eachcol(hcolors)]) end return z_data, cscheme end img = load("heatmap-forum.jpeg"); zdata, cscheme = image2zvals(img; n_colors=64, maxiter=200, tol=1e-04); plt = heatmap(zdata[end:-1:1,:], c=cgrad(cscheme), cbar=false, size=(375, 300)) zdata