0

How can I get the average value/a selection of 'prominent' colors from a nannou::wgpu::Texture?

I'm developing a simple utility for processing image files and would like to color the background around the image using it's average/most prominent color (like in a color palette). I'm mostly confident with Rust itself, but the graphics processing pipeline, wgsl and such go over my head right now.

Luctins
  • 77
  • 1
  • 6
  • "average" and "most prominent" are two different things. Would you know how to determine each of them without Rust/Nannou/wgsl? – mkrieger1 Jan 13 '23 at 22:45
  • I would like to get the average, but if there's some other method to get a 'color palette' from a Texture, that would be appreciated. – Luctins Jan 13 '23 at 22:57
  • And can you not get the average in the usual way by taking the sum of all pixels and dividing by the number of pixels? – mkrieger1 Jan 13 '23 at 22:59
  • I don't have a clear idea how to get raw access to the texture's pixel's. – Luctins Jan 13 '23 at 23:00
  • 1
    It would be easier to do this by just loading the image (e.g. with [image](https://docs.rs/image/)) and accessing the resulting vector of pixels that than by involving the GPU at all. Do you have a need to involve GPU processing here? – Kevin Reid Jan 14 '23 at 04:27
  • 1
    @Luctins you can use [`Texture::to_buffer`](https://docs.rs/nannou/latest/nannou/wgpu/struct.Texture.html#method.to_buffer) to get acess to the raw data. Then you could use something like [`kmeans_colors`](https://crates.io/crates/kmeans_colors) like shown [here](https://stackoverflow.com/a/73590722/4593433) to actually get a color pallette of dominant colors. – frankenapps Jan 14 '23 at 09:10
  • @frankenapps send this as a solution and I will gladly accept it. that was exactly what I was looking for. – Luctins Jan 14 '23 at 16:32
  • @KevinReid because AFAIK drawing `Texture`s is easier. I initially tried loading a image, but it seems for it to be drawn it eventually has to be converted to a texture. – Luctins Jan 14 '23 at 16:34

0 Answers0