|
2 | 2 | from PIL import Image |
3 | 3 | import numpy as np |
4 | 4 | import PySimpleGUI as sg |
5 | | -font_size = 6 |
6 | | -USING_QT = False |
7 | 5 |
|
8 | 6 | """ |
9 | 7 | Interesting program that shows your webcam's image as ASCII text. Runs in realtime, producing a stream of |
|
22 | 20 | pip install opencv-python |
23 | 21 | |
24 | 22 | On Linux / Mac use pip3 instead of pip |
| 23 | + |
| 24 | + Copyright 2022, PySimpleGUI |
25 | 25 | """ |
26 | 26 |
|
27 | 27 | # The magic bits that make the ASCII stuff work shamelessly taken from https://gist.github.com/cdiener/10491632 |
28 | 28 | chars = np.asarray(list(' .,:;irsXA253hMHGS#9B&@')) |
29 | 29 | SC, GCF, WCF = .1, 1, 7/4 |
30 | 30 |
|
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 |
32 | 33 |
|
33 | 34 | # define the window layout |
34 | 35 | # number of lines of text elements. Depends on cameras image size and the variable SC (scaller) |
35 | 36 | 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)] |
43 | 37 |
|
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')]] |
51 | 42 |
|
52 | 43 | # 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) |
55 | 45 |
|
56 | 46 | # ---===--- Event LOOP Read and display frames, operate the GUI --- # |
57 | 47 | # Setup the OpenCV capture device (webcam) |
| 48 | + |
58 | 49 | cap = cv2.VideoCapture(0) |
| 50 | + |
59 | 51 | while True: |
60 | 52 |
|
61 | 53 | event, values = window.read(timeout=0) |
|
66 | 58 |
|
67 | 59 | img = Image.fromarray(frame) # create PIL image from frame |
68 | 60 | GCF = float(values['-SPIN-GCF-']) |
69 | | - WCF = values['-SLIDER-WCF-'] |
| 61 | + WCF = 1.75 |
70 | 62 | # More magic that coverts the image to ascii |
71 | 63 | S = (round(img.size[0] * SC * WCF), round(img.size[1] * SC)) |
72 | 64 | img = np.sum(np.asarray(img.resize(S)), axis=2) |
73 | 65 | img -= img.min() |
74 | 66 | img = (1.0 - img / img.max()) ** GCF * (chars.size - 1) |
75 | 67 |
|
76 | 68 | # "Draw" the image in the window, one line of text at a time! |
| 69 | + font_size = int(values['-FONT SIZE-']) |
77 | 70 | 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)) |
79 | 72 |
|
80 | 73 | window.close() |
0 commit comments