From bd7cd64cee5f1ddc28e8e24e67b0aba393a4b3a3 Mon Sep 17 00:00:00 2001 From: jackyOO7 <44204857+jackyOO7@users.noreply.github.com> Date: Tue, 16 Oct 2018 17:26:26 +0200 Subject: [PATCH 1/2] Colour Image OpenCV->PySimpleGUI I think I found a nicer way to do it. PySimpleGUI is really neat, thank you for your hard work! Cheers jacky --- Demo_OpenCV_Webcam.py | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/Demo_OpenCV_Webcam.py b/Demo_OpenCV_Webcam.py index c21eaea0..006c2c47 100644 --- a/Demo_OpenCV_Webcam.py +++ b/Demo_OpenCV_Webcam.py @@ -4,10 +4,8 @@ if sys.version_info[0] >= 3: import PySimpleGUI as sg else: import PySimpleGUI27 as sg -import cv2 as cv -from PIL import Image +import cv2 import numpy as np -import io from sys import exit as exit """ @@ -31,7 +29,7 @@ def main(): window.Layout(layout).Finalize() # ---===--- Event LOOP Read and display frames, operate the GUI --- # - cap = cv.VideoCapture(0) + cap = cv2.VideoCapture(0) recording = False while True: event, values = window.ReadNonBlocking() @@ -42,10 +40,8 @@ def main(): recording = True elif event == 'Stop': recording = False - img = Image.new('RGB', (640, 480), (255, 255, 255)) - bio = io.BytesIO() # a binary memory resident stream - img.save(bio, format='PNG') # save image as png to it - imgbytes = bio.getvalue() + img = np.full((480, 640),255) + imgbytes=cv2.imencode('.png', img)[1].tobytes() #this is faster, shorter and needs less includes window.FindElement('image').Update(data=imgbytes) elif event == 'About': sg.PopupNoWait('Made with PySimpleGUI', @@ -56,14 +52,7 @@ def main(): keep_on_top=True) if recording: ret, frame = cap.read() - - gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) - - # let img be the PIL image - img = Image.fromarray(gray) # create PIL image from frame - bio = io.BytesIO() # a binary memory resident stream - img.save(bio, format= 'PNG') # save image as png to it - imgbytes = bio.getvalue() # this can be used by OpenCV hopefully + imgbytes=cv2.imencode('.png', cv2.cvtColor(frame,cv2.COLOR_BGR2RGB))[1].tobytes() #ditto window.FindElement('image').Update(data=imgbytes) -main() \ No newline at end of file +main() From 03ef2aae4ee6ced6598600fa7cdcc4e9a21b3ea1 Mon Sep 17 00:00:00 2001 From: jackyOO7 <44204857+jackyOO7@users.noreply.github.com> Date: Wed, 17 Oct 2018 07:38:53 +0200 Subject: [PATCH 2/2] Colour fix fixed the colours. cvtColor is not needed here as openCV takes care of the BGR issue when saving to PNG. --- Demo_OpenCV_Webcam.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Demo_OpenCV_Webcam.py b/Demo_OpenCV_Webcam.py index 006c2c47..c6fb89c3 100644 --- a/Demo_OpenCV_Webcam.py +++ b/Demo_OpenCV_Webcam.py @@ -52,7 +52,7 @@ def main(): keep_on_top=True) if recording: ret, frame = cap.read() - imgbytes=cv2.imencode('.png', cv2.cvtColor(frame,cv2.COLOR_BGR2RGB))[1].tobytes() #ditto + imgbytes=cv2.imencode('.png', frame)[1].tobytes() #ditto window.FindElement('image').Update(data=imgbytes) main()