# From the excellent WaveShapePlay tutorial here https://youtu.be/qg9sO4AD8-A

import serial
import time
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2

ser = serial.Serial('/dev/ttyACM1',9600)
time.sleep(1)
arduinoData = ser.readline().decode() # grab the latest line from the serial port and store it in arduinoData

# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (4000,3008)
rawCapture = PiRGBArray(camera)
time.sleep(0.1) # allow the camera to warmup

# grab an image from the camera
camera.capture(rawCapture, format="bgr")
image = rawCapture.array
image = cv2.resize(image,(1000,752),interpolation = cv2.INTER_AREA)

# display the image on screen and wait for a keypress
#cv2.namedWindow('Image',cv2.WINDOW_NORMAL)
#cv2.resizeWindow('Image',400,300)
cv2.imshow("Image", image)
cv2.waitKey(0)

filename = "{}.jpg".format(arduinoData)
filename = filename.replace('\r\n','')
print(filename)
cv2.imwrite(filename, image)
