Skip to content

Instantly share code, notes, and snippets.

@jath03
Created September 21, 2020 17:25
Show Gist options
  • Save jath03/a775de26d03812ee58faa94593955f1b to your computer and use it in GitHub Desktop.
Save jath03/a775de26d03812ee58faa94593955f1b to your computer and use it in GitHub Desktop.
Sync the background color from the game Hue to OpenRGB with python and opencv
from PIL import ImageGrab
import numpy as np
from Xlib import display
import cv2
from openrgb import OpenRGBClient
from openrgb.utils import RGBColor
d = display.Display()
s = d.screen()
x, y = s.width_in_pixels, s.height_in_pixels
cli = OpenRGBClient()
colors = {
# color name: (lower bound, upper bound, color to set lights to)
'red': ((175, 100, 200), (180, 255, 255), (255, 0, 0)),
'orange': ((0, 100, 200), (20, 255, 255), (255, 50, 0)),
'yellow': ((22, 200, 200), (35, 255, 255), (255, 255, 0)),
'green': ((40, 100, 200), (55, 255, 255), (0, 255, 0)),
'lblue': ((60, 100, 200), (100, 255, 255), (0, 255, 255)),
'dblue': ((100, 100, 200), (130, 255, 255), (0, 0, 255)),
'purple': ((135, 150, 200), (150, 255, 255), (150, 0, 255)),
'pink': ((150, 100, 200), (165, 255, 255), (255, 0, 255))
}
def find_bg_color(hsv):
sizes = []
for name, color in colors.items():
mask = cv2.inRange(hsv, color[0], color[1])
# cv2.imshow(name, mask)
sizes.append(cv2.countNonZero(mask))
if max(sizes) < 20000:
return (0, 0, 0)
return list(colors.values())[sizes.index(max(sizes))][2]
while(True):
# Just find some way to grab the game window. This works for my monitor configuration
img = ImageGrab.grab(bbox=(x - 2560, 240, x, y - int((1920 - 1440)/2)))
img_np = np.array(img)
frame = cv2.resize(img_np, (640, 360))
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
th, mask = cv2.threshold(gray, 20, 255, cv2.THRESH_BINARY)
color_thresh = cv2.bitwise_and(frame, frame, mask=mask)
hsv = cv2.cvtColor(color_thresh, cv2.COLOR_BGR2HSV)
cli.set_color(RGBColor(*find_bg_color(hsv)))
# cv2.imshow("mask", mask)
# cv2.imshow("test", gray)
# cv2.imshow("colors", color_thresh)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment