forked from mvanveen/mcut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmediancut.py
31 lines (22 loc) · 837 Bytes
/
mediancut.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from PIL import Image
from cube import ColorCube
def get_colors(image):
colors = image.getcolors(image.size[0] * image.size[1])
return [c[1] for c in colors]
def median_cut(image, num_colors):
colors = get_colors(image)
cubes = [ColorCube(*colors)]
while len(cubes) < num_colors:
global_max_size = 0
for index, cube in enumerate(cubes):
size = cube.size
max_size = max(size)
max_dim = size.index(max_size)
if max_size > global_max_size:
global_max_size = max_size
max_cube = index
split_box = cubes[max_cube]
cube_a, cube_b = split_box.split(max_dim)
cubes = cubes[:max_cube] + [cube_a, cube_b] + cubes[max_cube + 1:]
import pdb; pdb.set_trace()
return [c.average for c in cubes]