Created
November 30, 2012 21:15
-
-
Save springmeyer/4178690 to your computer and use it in GitHub Desktop.
use mapnik.Image to dump out all rgba colors in an image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import mapnik | |
import sys | |
def pixel2channels(pixel): | |
alpha = (pixel >> 24) & 0xff | |
red = pixel & 0xff | |
green = (pixel >> 8) & 0xff | |
blue = (pixel >> 16) & 0xff | |
return red,green,blue,alpha | |
def pixel2rgba(pixel): | |
return '%s,%s,%s,%s' % pixel2channels(pixel) | |
def get_unique_colors(im): | |
pixels = [] | |
for x in range(im.width()): | |
for y in range(im.height()): | |
pixel = im.get_pixel(x,y) | |
if pixel not in pixels: | |
pixels.append(pixel) | |
pixels = sorted(pixels) | |
return map(pixel2rgba,pixels) | |
if __name__ == "__main__": | |
image = sys.argv[1] | |
im = mapnik.Image.open(image); | |
for idx,c in enumerate(get_unique_colors(im)): | |
print idx,c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment