Skip to content

Commit 3f76d68

Browse files
authored
Merge pull request PySimpleGUI#5632 from PySimpleGUI/Dev-latest
Simplified code. Made font size selectable using a slider. Removed US…
2 parents 4591fb7 + b65b451 commit 3f76d68

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

DemoPrograms/Demo_OpenCV_Webcam_ASCII.py

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
from PIL import Image
33
import numpy as np
44
import PySimpleGUI as sg
5-
font_size = 6
6-
USING_QT = False
75

86
"""
97
Interesting program that shows your webcam's image as ASCII text. Runs in realtime, producing a stream of
@@ -22,40 +20,34 @@
2220
pip install opencv-python
2321
2422
On Linux / Mac use pip3 instead of pip
23+
24+
Copyright 2022, PySimpleGUI
2525
"""
2626

2727
# The magic bits that make the ASCII stuff work shamelessly taken from https://gist.github.com/cdiener/10491632
2828
chars = np.asarray(list(' .,:;irsXA253hMHGS#9B&@'))
2929
SC, GCF, WCF = .1, 1, 7/4
3030

31-
sg.theme('Black') # make it look cool
31+
sg.theme('Black') # make it look cool with white chars on black background
32+
font_size = 6
3233

3334
# define the window layout
3435
# number of lines of text elements. Depends on cameras image size and the variable SC (scaller)
3536
NUM_LINES = 48
36-
if USING_QT:
37-
layout = [[sg.Text(i, size_px=(800, 12),
38-
font=('Courier', font_size),
39-
key='-OUT-' + str(i))] for i in range(NUM_LINES)]
40-
else:
41-
layout = [[sg.Text(i, size=(120, 1), font=('Courier', font_size),
42-
pad=(0, 0), key='-OUT-'+str(i))] for i in range(NUM_LINES)]
4337

44-
layout += [[sg.Button('Exit', size=(5, 1)),
45-
sg.Text('GCF', size=(4, 1)),
46-
sg.Spin([round(i, 2) for i in np.arange(0.1, 20.0, 0.1)],
47-
initial_value=1, key='-SPIN-GCF-', size=(5, 1)),
48-
sg.Text('WCF', size=(4, 1)),
49-
sg.Slider((1, 4), resolution=.05, default_value=1.75,
50-
orientation='h', key='-SLIDER-WCF-', size=(15, 15))]]
38+
layout = [[[sg.Text(i, font=('Courier', font_size), pad=(0, 0), key=('-OUT-', i))] for i in range(NUM_LINES)],
39+
[sg.Text('GCF', s=9, justification='r'), sg.Slider((0.1, 20), resolution=.05, default_value=1, orientation='h', key='-SPIN-GCF-', size=(15, 15))],
40+
[sg.Text('Font Size', s=9, justification='r'), sg.Slider((4, 20), resolution=1, default_value=font_size, orientation='h', key='-FONT SIZE-', size=(15, 15)),
41+
sg.Push(), sg.Button('Exit')]]
5142

5243
# create the window and show it without the plot
53-
window = sg.Window('Demo Application - OpenCV Integration', layout,
54-
location=(800, 400), font='Any 18')
44+
window = sg.Window('Demo Application - OpenCV - ASCII Chars Output', layout, font='Any 18', resizable=True)
5545

5646
# ---===--- Event LOOP Read and display frames, operate the GUI --- #
5747
# Setup the OpenCV capture device (webcam)
48+
5849
cap = cv2.VideoCapture(0)
50+
5951
while True:
6052

6153
event, values = window.read(timeout=0)
@@ -66,15 +58,16 @@
6658

6759
img = Image.fromarray(frame) # create PIL image from frame
6860
GCF = float(values['-SPIN-GCF-'])
69-
WCF = values['-SLIDER-WCF-']
61+
WCF = 1.75
7062
# More magic that coverts the image to ascii
7163
S = (round(img.size[0] * SC * WCF), round(img.size[1] * SC))
7264
img = np.sum(np.asarray(img.resize(S)), axis=2)
7365
img -= img.min()
7466
img = (1.0 - img / img.max()) ** GCF * (chars.size - 1)
7567

7668
# "Draw" the image in the window, one line of text at a time!
69+
font_size = int(values['-FONT SIZE-'])
7770
for i, r in enumerate(chars[img.astype(int)]):
78-
window['-OUT-'+str(i)].update("".join(r))
71+
window[('-OUT-', i)].update("".join(r), font=('Courier', font_size))
7972

8073
window.close()

0 commit comments

Comments
 (0)