-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add dominant color method #4874
Changes from 8 commits
eda259b
f7afdda
6f3e77b
9c0f98a
8cce088
37ff4b9
c48e871
21315e5
5593b62
7725aa5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from .helper import hopper | ||
|
||
|
||
def test_getdominantcolors(): | ||
def getdominantcolors(mode): | ||
im = hopper(mode) | ||
colors = im.getdominantcolors() | ||
return len(colors) | ||
|
||
assert getdominantcolors("F") == 3 | ||
assert getdominantcolors("I") == 3 | ||
assert getdominantcolors("L") == 3 | ||
assert getdominantcolors("P") == 3 | ||
assert getdominantcolors("RGB") == 3 | ||
assert getdominantcolors("YCbCr") == 3 | ||
assert getdominantcolors("CMYK") == 3 | ||
assert getdominantcolors("RGBA") == 3 | ||
assert getdominantcolors("HSV") == 3 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can improve these tests so they're testing more than just they return a thing of length three. We can also use Here's an example: import pytest
from .helper import hopper
@pytest.mark.parametrize(
"test_mode, expected",
[
("F", [28.8104386146252, 66.26185757773263, 127.53211228743844]),
("I", [136, 94, 40]),
("L", [25, 63, 127]),
("P", [11, 71, 159]),
("RGB", [(172, 117, 94), (53, 44, 55), (95, 127, 185)]),
("YCbCr", [(130, 108, 155), (123, 163, 105), (47, 131, 131)]),
("CMYK", [(201, 210, 199, 0), (159, 127, 69, 0), (82, 137, 160, 0)]),
("RGBA", [(31, 24, 37, 255), (129, 125, 150, 255), (87, 67, 72, 255)]),
("HSV", [(140, 131, 85), (177, 70, 46), (97, 131, 186)]),
],
)
def test_getdominantcolors(test_mode, expected):
def getdominantcolors(mode):
im = hopper(mode)
im.thumbnail((10, 10))
colors = im.getdominantcolors()
return colors
assert getdominantcolors(test_mode) == expected What do you think? Do those expected return values look right? Might they be different on other systems? Especially the first one. If so, we can adjust it somewhat, possibly have a special case for that. It would be good to add further There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think my earlier comment never posted. The values for HSV are way off, straight up wrong even when I tested yesterday. I think the way I implemented the algorithm doesn't play nice with the way I approximate the centers of that color space. In addition when I convert CMYK and YCbCr to RGB they tend to be off by a few color values... This isn't too severe of a problem though. One solution to the HSV case is to simply convert to RGBA and do the calculations there... I'm going to see if I could fix what's happening before I divert to this option. My 2nd concern is that the returned colors are mostly subjective to the viewer. If say we used some other implementation as a base case, k-means may start out randomized and return slightly different results. Perhaps I could test within a range of accepted values? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see what you mean about it being slow,
pytest Tests/test_image_getdominantcolors.py
takes ~5.67s on my Mac.Resizing to
(10, 10)
reduces it to 0.23s.Resizing to
(20, 20)
reduces it to 0.41s.Do you think 10x10 might be too small to be useful?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small sizes like that give a very rough approximation especially if you're looking for a palette that's more than 3 colors. The colors returned tend to be much darker. I found that
(100,100)
gives somewhat decent results. The issue becomes exacerbated with large images (1080p wallpapers)