# -*- coding: utf-8 -*-
"""
Created on Thu Sep 12 14:19:39 2019

@author: Emile
"""
from win32api import GetSystemMetrics #in order to get the screen res
import PIL.ImageGrab as img #OSX and Windows only for Linux use: import pyscreenshot as img
import PIL.Image as Image
import numpy as np
import matplotlib.pyplot as plt
import serial


#Resizing the screen to get a 16*16 image so I can get pixels color without much processing

Width = GetSystemMetrics(0)
Height = GetSystemMetrics(1)
width = int(16)
height = int(16)   

#Launching the serial communication with arduino

ser = serial.Serial('COM3',9600)  # open serial port
print(ser.name)         # check which port was really used

#Infinite loop

while(True):
    if (ser.in_waiting):
        ser.flush()
        # Pix is a 16*16 image of the cureent screen
        rgb = (img.grab()).resize((width, height), Image.NEAREST) 
        pix = np.array(rgb)
        
        #Creating a 56*3 array containing the color value for each led
        data=np.concatenate((pix[-1,3::-1],pix[:,0][-1::-1],pix[0],pix[:,-1],pix[-1,-5:-1])).reshape(168)
        #data=np.concatenate((np.array([[0,0,255]]*16),np.array([[0,255,0]]*24),np.array([[255,0,0]]*16))).reshape(168)
        #Sending it to the arduino
        ser.write(bytearray(data.tolist()))
        

### How to deal with black strips, if you are looking a moovie in 21:9 on a 16:9 screen for example