Major update of all demo programs to use new PEP8 bindings, etc

This commit is contained in:
PySimpleGUI 2019-10-23 16:10:03 -04:00
parent 3f7c87c562
commit 7f52778bcc
307 changed files with 19546 additions and 3297 deletions

View file

@ -1,9 +1,9 @@
import cv2
from PIL import Image
import numpy as np
import PySimpleGUI as sg; font_size=6; USING_QT=False
# import PySimpleGUIQt as sg; font_size=8; USING_QT=True # if using, be sure and use the second layout that is commented out
# import PySimpleGUIWeb as sg; font_size=12; USING_QT=False # yes, it runs in a webpage too! Not as good as tkinter but works
import cv2
import PySimpleGUI as sg
font_size = 6
USING_QT = False
"""
Interesting program that shows your webcam's image as ASCII text. Runs in realtime, producing a stream of
@ -28,33 +28,45 @@ import cv2
chars = np.asarray(list(' .,:;irsXA253hMHGS#9B&@'))
SC, GCF, WCF = .1, 1, 7/4
sg.ChangeLookAndFeel('Black') # make it look cool
sg.change_look_and_feel('Black') # make it look cool
# define the window layout
NUM_LINES = 48 # number of lines of text elements. Depends on cameras image size and the variable SC (scaller)
# number of lines of text elements. Depends on cameras image size and the variable SC (scaller)
NUM_LINES = 48
if USING_QT:
layout = [[sg.T(i, size_px=(800, 12), font=('Courier', font_size), key='_OUT_' + str(i))] for i in range(NUM_LINES)]
layout = [[sg.Text(i, size_px=(800, 12),
font=('Courier', font_size),
key='-OUT-' + str(i))] for i in range(NUM_LINES)]
else:
layout = [[sg.T(i,size=(120,1), font=('Courier', font_size), pad=(0,0), key='_OUT_'+str(i))] for i in range(NUM_LINES)]
layout = [[sg.Text(i, size=(120, 1), font=('Courier', font_size),
pad=(0, 0), key='-OUT-'+str(i))] for i in range(NUM_LINES)]
layout += [[ sg.Button('Exit', size=(5,1)),
sg.T('GCF', size=(4,1)), sg.Spin([round(i,2) for i in np.arange(0.1,20.0,0.1)], initial_value=1, key='_SPIN_GCF_', size=(5,1)),
sg.T('WCF', size=(4,1)), sg.Slider((1,4), resolution=.05, default_value=1.75, orientation='h', key='_SLIDER_WCF_', size=(15,15))]]
layout += [[sg.Button('Exit', size=(5, 1)),
sg.Text('GCF', size=(4, 1)),
sg.Spin([round(i, 2) for i in np.arange(0.1, 20.0, 0.1)],
initial_value=1, key='-SPIN-GCF-', size=(5, 1)),
sg.Text('WCF', size=(4, 1)),
sg.Slider((1, 4), resolution=.05, default_value=1.75,
orientation='h', key='-SLIDER-WCF-', size=(15, 15))]]
# create the window and show it without the plot
window = sg.Window('Demo Application - OpenCV Integration', layout, location=(800,400), font='Any 18')
window = sg.Window('Demo Application - OpenCV Integration', layout,
location=(800, 400), font='Any 18')
# ---===--- Event LOOP Read and display frames, operate the GUI --- #
cap = cv2.VideoCapture(0) # Setup the OpenCV capture device (webcam)
# Setup the OpenCV capture device (webcam)
cap = cv2.VideoCapture(0)
while True:
event, values = window.Read(timeout=0)
event, values = window.read(timeout=0)
if event in ('Exit', None):
break
ret, frame = cap.read() # Read image from capture device (camera)
# Read image from capture device (camera)
ret, frame = cap.read()
img = Image.fromarray(frame) # create PIL image from frame
GCF = float(values['_SPIN_GCF_'])
WCF = values['_SLIDER_WCF_']
GCF = float(values['-SPIN-GCF-'])
WCF = values['-SLIDER-WCF-']
# More magic that coverts the image to ascii
S = (round(img.size[0] * SC * WCF), round(img.size[1] * SC))
img = np.sum(np.asarray(img.resize(S)), axis=2)
@ -63,5 +75,6 @@ while True:
# "Draw" the image in the window, one line of text at a time!
for i, r in enumerate(chars[img.astype(int)]):
window.Element('_OUT_'+str(i)).Update("".join(r))
window.Close()
window['-OUT-'+str(i)].update("".join(r))
window.close()