Last active
December 14, 2018 20:02
-
-
Save UltraWelfare/59a369896dc6ffb1c474451ff8119e2a to your computer and use it in GitHub Desktop.
Grab mean color of screen in python instantly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mss | |
from PIL import Image, ImageStat | |
# From what monitor to take screenshot | |
monitor_index = 0 | |
def get_color(rescale=(0, 0)): | |
# Returns a list with the RGBA values. | |
# Add a rescale tuple parameter (x,y) other than 0,0 to make faster calculations | |
with mss.mss() as sct: | |
# Grab raw pixels of the screen with sct. | |
sct_img = sct.grab(sct.monitors[monitor_index]) | |
# Convert to PIL Image to resize it to a lower resolution for faster calculations. | |
img = Image.frombytes("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX") | |
if rescale != (0, 0): | |
img = img.resize(rescale) | |
# Use the ImageStat module to get the mean value. | |
return ImageStat.Stat(img).mean |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks to /u/tjarko from reddit for bringing ImageStat to my attention!