Skip to content

Compute the most background & common color in an image #399

Answered by jcupitt
renchap asked this question in Q&A
Discussion options

You must be logged in to vote

🤦 I see what you mean, we could have one pixel containing two maxima.

You could add all v, r, g, b for a pixel, then sort by v and take the top 10. Perhaps:

#!/usr/bin/ruby

require "vips"

BINS = 16

def rgb_from_xyv(image, x, y)
  pixel = image.getpoint(x, y)
  pixel.map.with_index do |v, z|
    r = (x + 0.5) * 256 / BINS
    g = (y + 0.5) * 256 / BINS
    b = (z + 0.5) * 256 / BINS
    [v, r, g, b]
  end
end

def palette_from_image(image)
  histogram = image.hist_find_ndim(bins: BINS)
  _, colors = histogram.max(size: 10, out_array: true, x_array: true, y_array: true)

  maxima = colors['out_array'].map.with_index do |v, i|
    x = colors['x_array'][i]
    y = colors['y_array'][i]
    r…

Replies: 3 comments 13 replies

Comment options

You must be logged in to vote
6 replies
@renchap
Comment options

@jcupitt
Comment options

@jcupitt
Comment options

@Gargron
Comment options

@jcupitt
Comment options

Comment options

You must be logged in to vote
5 replies
@ClearlyClaire
Comment options

@jcupitt
Comment options

@ClearlyClaire
Comment options

@jcupitt
Comment options

Answer selected by renchap
@ClearlyClaire
Comment options

Comment options

You must be logged in to vote
2 replies
@ClearlyClaire
Comment options

@jcupitt
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants