Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
MohammadReza Sharifi
Published © MIT

Changing ambient light based on the color of monitor image

In this tutorial we will learn how to change ambient light and color using pillow and yeelight smart light.

BeginnerFull instructions provided1 hour166
Changing ambient light based on the color of monitor image

Things used in this project

Hardware components

Colour & Light Sensor, Flora RGB Smart NeoPixels
Colour & Light Sensor, Flora RGB Smart NeoPixels
I used xiaomi smart light bulb(yeelight smart light), you can use other smart light bulbs or RGB rings with arduino
×1

Software apps and online services

VS Code
Microsoft VS Code

Story

Read more

Code

main script based on python

Python
this python script calculates average color of monitor image and changes color of smart light bulb using python and pillow
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))

Credits

MohammadReza Sharifi
13 projects • 8 followers
I'm an Electrical Engineer and Maker.
Contact

Comments

Please log in or sign up to comment.