Skip to main content

Extracting Colors from Images in Python using the colorgram.py module

Day 45

My Code:

import colorgram

colors = colorgram.extract("image.jpg",1000)
color_list = []

for _ in range (len(colors)):
color_list.append(colors[_].rgb)

print(color_list)

Output:

[Rgb(r=250, g=234, b=220), Rgb(r=216, g=103, b=19), Rgb(r=67, g=171, b=214), Rgb(r=231, g=167, b=72), Rgb(r=200, g=122, b=181), Rgb(r=23, g=11, b=6), Rgb(r=251, g=141, b=158), Rgb(r=213, g=98, b=71), Rgb(r=252, g=238, b=243), Rgb(r=245, g=193, b=65), Rgb(r=249, g=252, b=251), Rgb(r=183, g=135, b=53), Rgb(r=5, g=19, b=31), Rgb(r=18, g=6, b=11), Rgb(r=247, g=164, b=142), Rgb(r=235, g=245, b=247), Rgb(r=58, g=106, b=129), Rgb(r=122, g=161, b=143), Rgb(r=5, g=10, b=7), Rgb(r=117, g=80, b=101), Rgb(r=90, g=67, b=27), Rgb(r=191, g=97, b=119), Rgb(r=81, g=145, b=168), Rgb(r=92, g=52, b=43), Rgb(r=162, g=204, b=211), Rgb(r=97, g=42, b=72), Rgb(r=29, g=74, b=92), Rgb(r=90, g=158, b=150), Rgb(r=76, g=100, b=88), Rgb(r=43, g=63, b=92), Rgb(r=170, g=204, b=202), Rgb(r=123, g=117, b=151), Rgb(r=48, g=71, b=69)]


To get rid of the rgbs from the output use:

import colorgram

colors = colorgram.extract("image.jpg",1000)
color_list = []

for _ in range (len(colors)):
r = colors[_].rgb.r
g = colors[_].rgb.g
b = colors[_].rgb.b
rgb = (r, g, b)
color_list.append(rgb)

print(color_list)


Comments

Popular posts from this blog

365 DAYS OF CODE - DAY 0

DAY 0 THOUGHT PROCESS Day Zero of the 365 days of code highlights my state of mind before the actual start of the program. Yes, it is a program. A program I set for myself to make sure I actually put in the work on a daily. I have tried coding before but have never been quite consistent with it. A friend advised me to try coding daily if I really wanted to be great at it someday. He also stated that it would take a long time and I should be prepared for it. During the same period, I noticed he was on a year's worth streak of chess which made me realize I could also do the same but with coding. I signed up for the 100 days of code course on Udemy and plan on starting my coding journey with Angela Yu and the thousands of students also taking the course. I also drafted my own copy of the pledge, copied and modified from the 100 Days of Python Pledge from the course materials. It is currently pasted on my wall as a constant reminder. Finally, I will also be recording my process here...

365 DAYS OF CODE - DAY 13

What is wrong with this code?? Can you tell what it does?? import art n1 = 0 n2 = 0 selected_operation = "" solution = 0 def add (n1, n2): return n1 + n2 def subtract (n1, n2): return n1 - n2 def multiply (n1, n2): return n1 * n2 def divide (n1, n2): return n1 / n2 operations = { "+" : add, "-" : subtract, "*" : multiply, "/" : divide } def rest_of_calculation (): print ( "+ \n - \n * \n /" ) selected_operation = input ( "Pick an operation: " ) n2 = float ( input ( "What is the next number?: " )) for key in operations: if key == selected_operation: solution = operations[key](n1, n2) print ( f" { n1 } { key } { n2 } = { solution } " ) print (art.logo) n1 = float ( input ( "What is the first number?: " )) rest_of_calculation() while True : new_calculation = input ( f"Type 'y' to continue calcu...

365 DAYS OF CODE - DAY 10

I realized that I have been tagging each day in accordance with the 100 days of python course . Now, I am one day behind (it is day 10 of the 365 days of code but day 9 on the python course I am taking right now) because I made sure to review unclear exercises yesterday. It is fine regardless. On day 9 of the python course, we looked at dictionaries, its table-likeness (2 columned) One tiny forgotten comma leads to 4 different errors and 1 warning, the prominent one being the SyntaxError. But shouldn’t it highlight line 3 instead of line 4?? Comma added, Error fixed. My Grading Program Code: student_scores = {     'Harry': 88,     'Ron': 78,     'Hermione': 95,     'Draco': 75,     'Neville': 60 }   student_grades = {}   for key in student_scores:     if student_scores[key] > 90:         student_scores[key] = "O...