Quantize. :param pixels: a list of pixel in the form (r, g, b) :param max_color: max number of colors
(pixels, max_color)
| 215 | |
| 216 | @staticmethod |
| 217 | def quantize(pixels, max_color): |
| 218 | """Quantize. |
| 219 | |
| 220 | :param pixels: a list of pixel in the form (r, g, b) |
| 221 | :param max_color: max number of colors |
| 222 | """ |
| 223 | if not pixels: |
| 224 | raise Exception('Empty pixels when quantize.') |
| 225 | if max_color < 2 or max_color > 256: |
| 226 | raise Exception('Wrong number of max colors when quantize.') |
| 227 | |
| 228 | histo = MMCQ.get_histo(pixels) |
| 229 | |
| 230 | # check that we aren't below maxcolors already |
| 231 | if len(histo) <= max_color: |
| 232 | # generate the new colors from the histo and return |
| 233 | pass |
| 234 | |
| 235 | # get the beginning vbox from the colors |
| 236 | vbox = MMCQ.vbox_from_pixels(pixels, histo) |
| 237 | pq = PQueue(lambda x: x.count) |
| 238 | pq.push(vbox) |
| 239 | |
| 240 | # inner function to do the iteration |
| 241 | def iter_(lh, target): |
| 242 | n_color = 1 |
| 243 | n_iter = 0 |
| 244 | while n_iter < MMCQ.MAX_ITERATION: |
| 245 | vbox = lh.pop() |
| 246 | if not vbox.count: # just put it back |
| 247 | lh.push(vbox) |
| 248 | n_iter += 1 |
| 249 | continue |
| 250 | # do the cut |
| 251 | vbox1, vbox2 = MMCQ.median_cut_apply(histo, vbox) |
| 252 | if not vbox1: |
| 253 | raise Exception("vbox1 not defined; shouldn't happen!") |
| 254 | lh.push(vbox1) |
| 255 | if vbox2: # vbox2 can be null |
| 256 | lh.push(vbox2) |
| 257 | n_color += 1 |
| 258 | if n_color >= target: |
| 259 | return |
| 260 | if n_iter > MMCQ.MAX_ITERATION: |
| 261 | return |
| 262 | n_iter += 1 |
| 263 | |
| 264 | # first set of colors, sorted by population |
| 265 | iter_(pq, MMCQ.FRACT_BY_POPULATIONS * max_color) |
| 266 | |
| 267 | # Re-sort by the product of pixel occupancy times the size in |
| 268 | # color space. |
| 269 | pq2 = PQueue(lambda x: x.count * x.volume) |
| 270 | while pq.size(): |
| 271 | pq2.push(pq.pop()) |
| 272 | |
| 273 | # next set - generate the median cuts using the (npix * vol) sorting. |
| 274 | iter_(pq2, max_color - pq2.size()) |
no test coverage detected