Finally can show PIL images directly from OpenCV!! (No more temp file)

This commit is contained in:
MikeTheWatchGuy 2018-10-10 01:38:36 -04:00
parent 23b0bb893b
commit 2f5005f799
1 changed files with 8 additions and 13 deletions

View File

@ -6,8 +6,7 @@ else:
import PySimpleGUI27 as sg import PySimpleGUI27 as sg
import cv2 as cv import cv2 as cv
from PIL import Image from PIL import Image
import tempfile import io
import os
from sys import exit as exit from sys import exit as exit
""" """
@ -18,9 +17,7 @@ a temp file. Clearly... clearly... this is not the optimal solution and one is
Until then enjoy it working somewhat slowly. Until then enjoy it working somewhat slowly.
""" """
def main(): def main():
# filename = 'C:/Python/MIDIVideo/PlainVideos/- 08-30 Ted Talk/TED Talk Short - Video+.mp4'
filename = sg.PopupGetFile('Filename to play') filename = sg.PopupGetFile('Filename to play')
if filename is None: if filename is None:
exit(69) exit(69)
@ -38,18 +35,13 @@ def main():
[sg.ReadButton('Exit', size=(10, 2), pad=((600, 0), 3), font='Helvetica 14')]] [sg.ReadButton('Exit', size=(10, 2), pad=((600, 0), 3), font='Helvetica 14')]]
# create the window and show it without the plot # create the window and show it without the plot
window = sg.Window('Demo Application - OpenCV Integration', no_titlebar=False, location=(0,0)) window = sg.Window('Demo Application - OpenCV Integration', no_titlebar=False, location=(0,0)).Layout(layout)
window.Layout(layout)
window.ReadNonBlocking()
# ---===--- LOOP through video file by frame --- # # ---===--- LOOP through video file by frame --- #
i = 0 i = 0
temp_filename = next(tempfile._get_candidate_names()) + '.png'
while vidFile.isOpened(): while vidFile.isOpened():
button, values = window.ReadNonBlocking() button, values = window.ReadNonBlocking()
if button is 'Exit' or values is None: if button is 'Exit' or values is None:
os.remove(temp_filename)
exit(69) exit(69)
ret, frame = vidFile.read() ret, frame = vidFile.read()
if not ret: # if out of data stop looping if not ret: # if out of data stop looping
@ -58,9 +50,12 @@ def main():
window.FindElement('slider').Update(i) window.FindElement('slider').Update(i)
i += 1 i += 1
with open(temp_filename, 'wb') as f: # let img be the PIL image
Image.fromarray(frame).save(temp_filename, 'PNG') # save the PIL image as file img = Image.fromarray(frame) # create PIL image from frame
window.FindElement('image').Update(filename=temp_filename) 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)
main() main()