-
Notifications
You must be signed in to change notification settings - Fork 12
/
octree_quantizer.py
193 lines (172 loc) · 5.63 KB
/
octree_quantizer.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env python
#-*- coding: utf8 -*-
from color import Color
class OctreeNode(object):
"""
Octree Node class for color quantization
"""
def __init__(self, level, parent):
"""
Init new Octree Node
"""
self.color = Color(0, 0, 0)
self.pixel_count = 0
self.palette_index = 0
self.children = [None for _ in xrange(8)]
# add node to current level
if level < OctreeQuantizer.MAX_DEPTH - 1:
parent.add_level_node(level, self)
def is_leaf(self):
"""
Check that node is leaf
"""
return self.pixel_count > 0
def get_leaf_nodes(self):
"""
Get all leaf nodes
"""
leaf_nodes = []
for i in xrange(8):
node = self.children[i]
if node:
if node.is_leaf():
leaf_nodes.append(node)
else:
leaf_nodes.extend(node.get_leaf_nodes())
return leaf_nodes
def get_nodes_pixel_count(self):
"""
Get a sum of pixel count for node and its children
"""
sum_count = self.pixel_count
for i in xrange(8):
node = self.children[i]
if node:
sum_count += node.pixel_count
return sum_count
def add_color(self, color, level, parent):
"""
Add `color` to the tree
"""
if level >= OctreeQuantizer.MAX_DEPTH:
self.color.red += color.red
self.color.green += color.green
self.color.blue += color.blue
self.pixel_count += 1
return
index = self.get_color_index_for_level(color, level)
if not self.children[index]:
self.children[index] = OctreeNode(level, parent)
self.children[index].add_color(color, level + 1, parent)
def get_palette_index(self, color, level):
"""
Get palette index for `color`
Uses `level` to go one level deeper if the node is not a leaf
"""
if self.is_leaf():
return self.palette_index
index = self.get_color_index_for_level(color, level)
if self.children[index]:
return self.children[index].get_palette_index(color, level + 1)
else:
# get palette index for a first found child node
for i in xrange(8):
if self.children[i]:
return self.children[i].get_palette_index(color, level + 1)
def remove_leaves(self):
"""
Add all children pixels count and color channels to parent node
Return the number of removed leaves
"""
result = 0
for i in xrange(8):
node = self.children[i]
if node:
self.color.red += node.color.red
self.color.green += node.color.green
self.color.blue += node.color.blue
self.pixel_count += node.pixel_count
result += 1
return result - 1
def get_color_index_for_level(self, color, level):
"""
Get index of `color` for next `level`
"""
index = 0
mask = 0x80 >> level
if color.red & mask:
index |= 4
if color.green & mask:
index |= 2
if color.blue & mask:
index |= 1
return index
def get_color(self):
"""
Get average color
"""
return Color(
self.color.red / self.pixel_count,
self.color.green / self.pixel_count,
self.color.blue / self.pixel_count)
class OctreeQuantizer(object):
"""
Octree Quantizer class for image color quantization
Use MAX_DEPTH to limit a number of levels
"""
MAX_DEPTH = 8
def __init__(self):
"""
Init Octree Quantizer
"""
self.levels = {i: [] for i in xrange(OctreeQuantizer.MAX_DEPTH)}
self.root = OctreeNode(0, self)
def get_leaves(self):
"""
Get all leaves
"""
return [node for node in self.root.get_leaf_nodes()]
def add_level_node(self, level, node):
"""
Add `node` to the nodes at `level`
"""
self.levels[level].append(node)
def add_color(self, color):
"""
Add `color` to the Octree
"""
# passes self value as `parent` to save nodes to levels dict
self.root.add_color(color, 0, self)
def make_palette(self, color_count):
"""
Make color palette with `color_count` colors maximum
"""
palette = []
palette_index = 0
leaf_count = len(self.get_leaves())
# reduce nodes
# up to 8 leaves can be reduced here and the palette will have
# only 248 colors (in worst case) instead of expected 256 colors
for level in xrange(OctreeQuantizer.MAX_DEPTH - 1, -1, -1):
if self.levels[level]:
for node in self.levels[level]:
leaf_count -= node.remove_leaves()
if leaf_count <= color_count:
break
if leaf_count <= color_count:
break
self.levels[level] = []
# build palette
for node in self.get_leaves():
if palette_index >= color_count:
break
if node.is_leaf():
palette.append(node.get_color())
node.palette_index = palette_index
palette_index += 1
return palette
def get_palette_index(self, color):
"""
Get palette index for `color`
"""
return self.root.get_palette_index(color, 0)