168 lines
4.0 KiB
Python
Executable File
168 lines
4.0 KiB
Python
Executable File
#!/usr/bin/python3
|
|
import socket
|
|
import getopt
|
|
import sys
|
|
import subprocess
|
|
import pyscreenshot
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
import cv2
|
|
import numpy as np
|
|
from skimage import io
|
|
from threading import Thread
|
|
import time
|
|
|
|
s1 = socket.socket()
|
|
s2 = socket.socket()
|
|
server = ["192.168.188.64", "192.168.188.76"]
|
|
|
|
def connect(port = 5000):
|
|
s1.connect((server[0], port))
|
|
s2.connect((server[1], port))
|
|
def disconnect():
|
|
s1.close() # close the connection
|
|
s2.close()
|
|
def send(data):
|
|
s1.send(data.encode()) # send message
|
|
s2.send(data.encode()) # send message
|
|
|
|
def helpmenu():
|
|
print("light controll\n")
|
|
print("Options:")
|
|
print("-h show help")
|
|
print("-s <hex-color> set static color")
|
|
print("-v visualizer")
|
|
|
|
def visualizer():
|
|
cava = subprocess.Popen(["cava", "-p", "./cava.conf"], stdout=subprocess.PIPE)
|
|
sed = subprocess.Popen(["sed", "-u", "s/;.*;$//"], stdin=cava.stdout, stdout=subprocess.PIPE)
|
|
subprocess.Popen([sys.argv[0]], stdin=sed.stdout, shell=False)
|
|
cava.stdout.close()
|
|
sed.stdout.close()
|
|
|
|
_r,_g,_b = 0,0,0
|
|
|
|
def vibrant(r,g,b):
|
|
intensity = 30 # usabel range 1-100 max:1000
|
|
|
|
intensity = 1+intensity/1000
|
|
rgb = [r,g,b]
|
|
print(rgb)
|
|
#min_idx = rgb.index(min(rgb))
|
|
d = (r+g+b)/3
|
|
for c in range(3):
|
|
if rgb[c] < d:
|
|
rgb[c] = int(rgb[c]*(intensity**(rgb[c]-d)))
|
|
elif rgb[c] > d:
|
|
rgb[c] = int(rgb[c]*(-intensity**(-rgb[c]+d)+2))
|
|
#rgb[min_idx] = int(rgb[min_idx]*(rgb[min_idx]/d)**2)
|
|
print(rgb)
|
|
return rgb
|
|
|
|
def ambient_light_thread():
|
|
r,g,b = 0,0,0
|
|
brighness = 1
|
|
while True:
|
|
|
|
if r > _r:
|
|
r-=1
|
|
elif r < _r:
|
|
r+=1
|
|
|
|
if g > _g:
|
|
g-=1
|
|
elif g < _g:
|
|
g+=1
|
|
|
|
if b > _b:
|
|
b-=1
|
|
elif b < _b:
|
|
b+=1
|
|
|
|
print(r,g,b)
|
|
send(rgb_to_hex(int(r*brighness),int(g*brighness),int(b*brighness)))
|
|
time.sleep(0.01)
|
|
|
|
def ambient_light():
|
|
|
|
t = Thread(target=ambient_light_thread)
|
|
t.start()
|
|
|
|
global _r,_g,_b
|
|
|
|
while True:
|
|
# screenshot
|
|
img = pyscreenshot.grab(backend="mss", childprocess=False, bbox=(1920,0,4480,1440))
|
|
|
|
# find dominant color
|
|
'''
|
|
img = np.array(img)
|
|
pixels = np.float32(img.reshape(-1, 3))
|
|
img = cv2.UMat(img)
|
|
n_colors = 1
|
|
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 200, .1)
|
|
flags = cv2.KMEANS_RANDOM_CENTERS
|
|
_, labels, palette = cv2.kmeans(img, n_colors, None, criteria, 10, flags)
|
|
_, counts = np.unique(labels, return_counts=True)
|
|
dominant = palette[np.argmax(counts)]
|
|
_r,_g,_b = int(dominant[0]), int(dominant[1]), int(dominant[2])
|
|
'''
|
|
|
|
img.thumbnail((1,1))
|
|
r,g,b = img.getpixel((0, 0))
|
|
_r,_g,_b = vibrant(r,g,b)
|
|
|
|
def rgb_to_hex(r,g,b):
|
|
return "%02x%02x%02x" % (r,g,b)
|
|
|
|
def test():
|
|
for i in range(256):
|
|
h = rgb_to_hex(0,i,0)
|
|
send(h)
|
|
print(h)
|
|
time.sleep(0.0)
|
|
|
|
def main(argv):
|
|
if not sys.stdin.isatty():
|
|
connect()
|
|
for volume in sys.stdin:
|
|
volume = int(volume)
|
|
hex_color = rgb_to_hex(volume,0,0)
|
|
send(hex_color)
|
|
sys.exit()
|
|
|
|
try:
|
|
opts, args = getopt.getopt(argv, "s:vaht")
|
|
except getopt.GetoptError:
|
|
print(sys.argv[0], "invalid option")
|
|
print("Try", sys.argv[0], "-h for help")
|
|
sys.exit(1)
|
|
|
|
for opt, arg in opts:
|
|
if opt == "-h":
|
|
helpmenu()
|
|
sys.exit()
|
|
elif opt == "-s":
|
|
connect()
|
|
send(arg)
|
|
disconnect()
|
|
sys.exit()
|
|
elif opt == "-a":
|
|
connect()
|
|
ambient_light()
|
|
disconnect()
|
|
sys.exit()
|
|
elif opt == "-v":
|
|
visualizer()
|
|
sys.exit()
|
|
elif opt == "-t":
|
|
connect()
|
|
test()
|
|
disconnect()
|
|
sys.exit()
|
|
|
|
helpmenu()
|
|
sys.exit()
|
|
|
|
if __name__ == "__main__":
|
|
main(sys.argv[1:])
|