Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

get a list of colors that appear of the image python

'''First the defaultdict is populated with data from the image
using built-in getdata() method as follows:'''
from collections import defaultdict

by_color = defaultdict(int)
im = Image.open('myimage.jpg')
for pixel in im.getdata():
    by_color[pixel] += 1
#Then print the populated dictionary:    
print(by_color)
#################################Output################################
defaultdict(<class 'int'>, {(123, 131, 82): 31, (121, 131, 81): 24,...})
#Where the pixel, (123, 131, 82) is the key and the value 31 indicates-
#how many times this pixel appears on the image. 
 
PREVIOUS NEXT
Tagged: #list #colors #image #python
ADD COMMENT
Topic
Name
4+8 =