Python PIL | getcolors() Method

PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files, and to create new images.

getcolors() Returns a list of colors used in this image.

Syntax: Image.getcolors(maxcolors=256)



Parameters:
maxcolors – Maximum number of colors. If this number is exceeded, this method returns None. The default limit is 256 colors.

Returns: An unsorted list of (count, pixel) values.

Image Used:

filter_none

edit
close

play_arrow

link
brightness_4
code

   
  
# importing Image module from PIL package 
from PIL import Image 
  
# opening a  image 
im = Image.open(r"C:\Users\System-Pc\Desktop\lion.png").convert("L"
  
# getting colors 
# multiband images (RBG) 
im1 = Image.Image.getcolors(im) 
  
print(im1) 

chevron_right


Output:

[(38510, 0), (27, 12), (17, 21), (2, 24), (34, 26), (46, 39), (30, 41), (18, 48), (42, 50), (58, 62), (27, 67), (37, 73), (51, 79), (46, 85), (57, 90), (58, 101), (21, 104), (41, 111), (30, 117), (48, 123), (50, 130), (42, 135), (60, 143), (63, 149), (63, 155), (49, 162), (25, 165), (65, 169), (58, 178), (67, 184), (61, 193), (61, 195), (65, 204), (61, 208), (61, 215), (50, 221), (59, 225), (49, 232), (81, 235), (80, 244), (56, 247), (64, 252), (9835, 255)]

Another Example:Here changing image.

Image Used:

filter_none

edit
close

play_arrow

link
brightness_4
code

   
  
# importing Image module from PIL package 
from PIL import Image 
  
# opening a  image 
im = Image.open(r"C:\Users\System-Pc\Desktop\new.jpg").convert("L"
  
# getting colors 
# multiband images (RBG) 
im1 = Image.Image.getcolors(im) 
  
print(im1) 

chevron_right


Output:

[(97, 0), (158, 1), (243, 2), (207, 3), (258, 4), (259, 5), (179, 6), (270, 7), (248, 8), (270, 9), (246, 10), (255, 11), (298, 12), (271, 13), (283, 14), (335, 15), (248, 16), (353, 17), (311, 18), (265, 19), (353, 20), (314, 21), (330, 22), (342, 23), (313, 24), (359, 25), (337, 26), (303, 27), (375, 28), (305, 29), (358, 30), (370, 31), (346, 32), (394, 33), (372, 34), (332, 35), (400, 36), (367, 37), (370, 38), (400, 39), (406, 40), (423, 41), (371, 42), (366, 43), (362, 44), (391, 45), (397, 46), (403, 47), (439, 48), (397, 49), (433, 50), (407, 51), (424, 52), (383, 53), (426, 54), (398, 55), (407, 56), (405, 57), (411, 58), (428, 59), (394, 60), (401, 61), (372, 62), (394, 63), (393, 64), (384, 65), (374, 66), (380, 67), (399, 68), (368, 69), (375, 70), (365, 71), (323, 72), (363, 73), (348, 74), (355, 75), (326, 76), (345, 77), (332, 78), (332, 79), (324, 80), (314, 81), (348, 82), (322, 83), (335, 84), (343, 85), (330, 86), (326, 87), (296, 88), (327, 89), (315, 90), (322, 91), (275, 92), (305, 93), (278, 94), (286, 95), (284, 96), (256, 97), (253, 98), (292, 99), (258, 100), (286, 101), (295, 102), (280, 103), (269, 104), (259, 105), (259, 106), (312, 107), (254, 108), (265, 109), (278, 110), (265, 111), (263, 112), (265, 113), (255, 114), (264, 115), (273, 116), (255, 117), (245, 118), (250, 119), (240, 120), (244, 121), (242, 122), (247, 123), (224, 124), (240, 125), (209, 126), (240, 127), (200, 128), (216, 129), (218, 130), (207, 131), (176, 132), (188, 133), (179, 134), (164, 135), (190, 136), (180, 137), (178, 138), (151, 139), (139, 140), (142, 141), (150, 142), (134, 143), (145, 144), (131, 145), (115, 146), (128, 147), (133, 148), (135, 149), (117, 150), (129, 151), (133, 152), (115, 153), (113, 154), (114, 155), (109, 156), (133, 157), (119, 158), (117, 159), (97, 160), (128, 161), (103, 162), (113, 163), (105, 164), (110, 165), (94, 166), (70, 167), (85, 168), (89, 169), (99, 170), (89, 171), (85, 172), (89, 173), (93, 174), (79, 175), (66, 176), (75, 177), (62, 178), (63, 179), (62, 180), (77, 181), (63, 182), (78, 183), (62, 184), (57, 185), (58, 186), (47, 187), (61, 188), (69, 189), (61, 190), (58, 191), (47, 192), (49, 193), (71, 194), (47, 195), (40, 196), (56, 197), (64, 198), (55, 199), (40, 200), (46, 201), (42, 202), (31, 203), (36, 204), (46, 205), (47, 206), (45, 207), (50, 208), (52, 209), (40, 210), (42, 211), (48, 212), (60, 213), (43, 214), (39, 215), (51, 216), (27, 217), (46, 218), (41, 219), (35, 220), (53, 221), (35, 222), (39, 223), (36, 224), (37, 225), (42, 226), (29, 227), (36, 228), (40, 229), (29, 230), (34, 231), (24, 232), (25, 233), (20, 234), (22, 235), (26, 236), (13, 237), (11, 238), (16, 239), (17, 240), (8, 241), (16, 242), (22, 243), (11, 244), (15, 245), (14, 246), (10, 247), (12, 248), (11, 249), (18, 250), (23, 251), (20, 252), (16, 253), (3, 254)]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.