Skip to content

Instantly share code, notes, and snippets.

@bradmontgomery
Last active October 1, 2019 16:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradmontgomery/613861 to your computer and use it in GitHub Desktop.
Save bradmontgomery/613861 to your computer and use it in GitHub Desktop.
Use PIL to find a list of colors in an image
import Image
def get_color_list(path_to_image):
"""
Returns a list of tuples for RGB colors: (R, G, B)
This is gonna be slow and memory-intensive. Use at your own risk.
Requires PIL
"""
def _get_color(t): return t[1]
im = Image.open(path_to_image)
colors = []
colors = map(_get_color, im.getcolors(im.size[0]*im.size[1]))
colors = list(set(colors)) # ditch duplicate colors
return colors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment