Merge pull request #492 from jackyOO7/patch-1
Colour Image OpenCV->PySimpleGUI
This commit is contained in:
commit
3e5657c2e7
|
@ -4,10 +4,8 @@ if sys.version_info[0] >= 3:
|
||||||
import PySimpleGUI as sg
|
import PySimpleGUI as sg
|
||||||
else:
|
else:
|
||||||
import PySimpleGUI27 as sg
|
import PySimpleGUI27 as sg
|
||||||
import cv2 as cv
|
import cv2
|
||||||
from PIL import Image
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import io
|
|
||||||
from sys import exit as exit
|
from sys import exit as exit
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
@ -31,7 +29,7 @@ def main():
|
||||||
window.Layout(layout).Finalize()
|
window.Layout(layout).Finalize()
|
||||||
|
|
||||||
# ---===--- Event LOOP Read and display frames, operate the GUI --- #
|
# ---===--- Event LOOP Read and display frames, operate the GUI --- #
|
||||||
cap = cv.VideoCapture(0)
|
cap = cv2.VideoCapture(0)
|
||||||
recording = False
|
recording = False
|
||||||
while True:
|
while True:
|
||||||
event, values = window.ReadNonBlocking()
|
event, values = window.ReadNonBlocking()
|
||||||
|
@ -42,10 +40,8 @@ def main():
|
||||||
recording = True
|
recording = True
|
||||||
elif event == 'Stop':
|
elif event == 'Stop':
|
||||||
recording = False
|
recording = False
|
||||||
img = Image.new('RGB', (640, 480), (255, 255, 255))
|
img = np.full((480, 640),255)
|
||||||
bio = io.BytesIO() # a binary memory resident stream
|
imgbytes=cv2.imencode('.png', img)[1].tobytes() #this is faster, shorter and needs less includes
|
||||||
img.save(bio, format='PNG') # save image as png to it
|
|
||||||
imgbytes = bio.getvalue()
|
|
||||||
window.FindElement('image').Update(data=imgbytes)
|
window.FindElement('image').Update(data=imgbytes)
|
||||||
elif event == 'About':
|
elif event == 'About':
|
||||||
sg.PopupNoWait('Made with PySimpleGUI',
|
sg.PopupNoWait('Made with PySimpleGUI',
|
||||||
|
@ -56,14 +52,7 @@ def main():
|
||||||
keep_on_top=True)
|
keep_on_top=True)
|
||||||
if recording:
|
if recording:
|
||||||
ret, frame = cap.read()
|
ret, frame = cap.read()
|
||||||
|
imgbytes=cv2.imencode('.png', frame)[1].tobytes() #ditto
|
||||||
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
|
|
||||||
window.FindElement('image').Update(data=imgbytes)
|
window.FindElement('image').Update(data=imgbytes)
|
||||||
|
|
||||||
main()
|
main()
|
Loading…
Reference in New Issue