elixir-image / image

Image processing for Elixir

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

dominant_color with PNG

mayel opened this issue · comments

Hello, is this function supposed to work with PNG images?
It works for me with JPEG but I keep getting this error trying with different PNG files: operation build: hist_find_ndim: image is not 1 - 3 bands

Ah, sorry about that. My mistake. Basically the error is saying you have an image with an alpha band and the underlying function can't handle that. I will fix that on the weekend but for now you can do:

image
|> Image.flatten!()
|> Image.dominant_color(bins: x)

And you should be good to go.

Let me know if that gets you moving forward?

I've published Image version 0.38.2 with the following changelog entry:

Bug Fixes

  • Fix Image.dominant_color/2 for images with an alpha band. Thanks to @mayel for the report. Closes #114.

Thanks for the report. Of course please re-open if this doesn't solve the problem for you.

@kipcole9 thanks for the speedy response and fix! So I indeed no longer get a crash for any of my test PNGs, but all of those with an alpha layer result in #DDD, whereas if I first use an app to re-save them without an alpha layer it then gives me different color codes.

That's clearly not good! I will investigate ASAP but I'm a bit time poor over the next 12 hours - just to be realistic on expectations.

Thanks! No hurry, I'm using this for a toy feature: showing a placeholder background for users who haven't uploaded a banner image:
Screenshot 2023-09-27 at 20 30 47

Reopening to see how well I can follow this advice and turn it into an acceptable solution.

Hey @kipcole9, I actually needed something very similar and I faced the same issue as while using Image.
So created a working one in Vix using the Jcupitt advice you mentioned above. If you are interested, I can create PR this week.

@akash-akya - thanks for the assist, really appreciate it. I wasn't sure if there was an alternative to mutating the histogram - your example convinced me it's ok to do that. I pushed an update to my Image.dominant_color/2 function leveraging your good work.

@mayel - I think this is now operating correctly. As you know, this is not a precise dominant color calculation. The dominance is calculated from a histogram which necessarily covers color ranges - not a precise color. You can increase the precision somewhat by using bins: n where 10 <= n <= 256 but the calculation time rises exponentially without necessarily returning a more useful result.

As always, please do re-open if this isn't operating as you expect. And feel free to attach a test image I can work with too.

I've taken way too long to get this issue addressed so I'd like to get a new release shipped by end of this week. I'm using the "its still not 1.0" as the excuse to keep iterating.

Thanks @kipcole9 @akash-akya :) This seems to produce good results with all my test images:
Screenshot 2023-12-14 at 10 26 23
Screenshot 2023-12-14 at 10 00 34

In fact it produces a result even with an SVG (though I'm less sure how valid this one is, the test image is https://commons.wikimedia.org/wiki/File:Fediverse_logo_proposal.svg):
Screenshot 2023-12-14 at 10 25 39

That's great to hear, thanks for the feedback. I may do a little tuning and and some more tests and then get it out the door.

Thanks very much for your patience, I shouldn't have taken this long to get it done. And I wouldn't have without the assist from @akash-akya.

No worries, thank you! :)