A few days ago, I was browsing Reddit when I happened to see a post by a user who had done a similar project, and changed the color of an LED strip behind the desk and monitor based on the color and lighting of the image. He had not given any explanation about his project and he had not shared his program and schematic. In the comments section, a user raised a question and asked how this project works. The person who completed the project replied: I take a screenshot of the monitor image and calculate the average color. That was enough for me to get the main clue and implement my idea on a small scale.
We all have seen this feature in smart and high-end TVs of big companies, and it is very interesting to be able to add this feature to our personal desk and monitor.
first of all we need to install two modules in python, pillow and yeelight.
pip install yeelight
pip install pillow
also you need to read this tutorial, to learn how to control yeelight smart light bulb using python.
now, just run this script and enjoy.
from PIL import ImageGrab, Image
import numpy as np
from yeelight import Bulb, LightType
from time import sleep
my_light = Bulb("192.168.124.157")
my_light.turn_on()
while True:
# Capture the entire screen
screenshot = ImageGrab.grab()
# Save the screenshot to a file
screenshot.save('pic.png', 'PNG')
# Load the image
img = Image.open('pic.png')
# Convert the image to RGB (if it's not already in that format)
img = img.convert('RGB')
# Use numpy to calculate the average
np_img = np.array(img)
average_color = np_img.mean(axis=(0, 1))
r, g, b = average_color
my_light.set_rgb(int(r),int(g),int(b))
sleep(1)
#print(int(r),int(g),int(b))
#print(f"The average color of the image is: {average_color}",type(average_color))
Comments
Please log in or sign up to comment.