|
1 | | -#!/usr/bin/python3 |
2 | | - |
3 | | -### pngtowav v1.0 |
4 | | -"""Convert a list of png images to pseudo composite video in wav file form. |
5 | | -
|
6 | | -This is Python code not intended for running on a microcontroller board. |
7 | | -""" |
8 | | - |
9 | | -### MIT License |
10 | | - |
11 | | -### Copyright (c) 2019 Kevin J. Walters |
12 | | - |
13 | | -### Permission is hereby granted, free of charge, to any person obtaining a copy |
14 | | -### of this software and associated documentation files (the "Software"), to deal |
15 | | -### in the Software without restriction, including without limitation the rights |
16 | | -### to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
17 | | -### copies of the Software, and to permit persons to whom the Software is |
18 | | -### furnished to do so, subject to the following conditions: |
19 | | - |
20 | | -### The above copyright notice and this permission notice shall be included in all |
21 | | -### copies or substantial portions of the Software. |
22 | | - |
23 | | -### THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
24 | | -### IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
25 | | -### FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
26 | | -### AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
27 | | -### LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
28 | | -### OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
29 | | -### SOFTWARE. |
30 | | - |
31 | | -import getopt |
32 | | -import sys |
33 | | -import array |
34 | | -import wave |
35 | | - |
36 | | -import imageio |
37 | | - |
38 | | - |
39 | | -### globals |
40 | | -### pylint: disable=invalid-name |
41 | | -### start_offset of 1 can help if triggering on oscilloscope |
42 | | -### is missing alternate lines |
43 | | -debug = 0 |
44 | | -verbose = False |
45 | | -movie_file = False |
46 | | -output_filename = "dacanim.wav" |
47 | | -fps = 50 |
48 | | -threshold = 128 ### pixel level |
49 | | -replaceforsync = False |
50 | | -start_offset = 1 |
51 | | - |
52 | | -max_dac_v = 3.3 |
53 | | -### 16 bit wav files always use signed representation for data |
54 | | -dac_offtop = 2**15-1 ### 3.30V |
55 | | -dac_sync = -2**15 ### 0.00V |
56 | | -### image from 3.00V to 0.30V |
57 | | -dac_top = round(3.00 / max_dac_v * (2**16-1)) - 2**15 |
58 | | -dac_bottom = round(0.30 / max_dac_v * (2**16-1)) - 2**15 |
59 | | - |
60 | | - |
61 | | -def usage(exit_code): ### pylint: disable=missing-docstring |
62 | | - print("pngtowav: " |
63 | | - + "[-d] [-f fps] [-h] [-m] [-o outputfilename] [-r] [-s lineoffset] [-t threshold] [-v]", |
64 | | - file=sys.stderr) |
65 | | - if exit_code is not None: |
66 | | - sys.exit(exit_code) |
67 | | - |
68 | | - |
69 | | -def image_to_dac(img, row_offset, first_pix, dac_y_range): |
70 | | - """Convert a single image to DAC output.""" |
71 | | - dac_out = array.array("h", []) |
72 | | - |
73 | | - img_height, img_width = img.shape |
74 | | - if verbose: |
75 | | - print("W,H", img_width, img_height) |
76 | | - |
77 | | - for row_o in range(img_height): |
78 | | - row = (row_o + row_offset) % img_height |
79 | | - ### Currently using 0 to (n-1)/n range |
80 | | - y_pos = round(dac_top - row / (img_height - 1) * dac_y_range) |
81 | | - if verbose: |
82 | | - print("Adding row", row, "at y_pos", y_pos) |
83 | | - dac_out.extend(array.array("h", |
84 | | - [dac_sync] |
85 | | - + [y_pos if x >= threshold else dac_offtop |
86 | | - for x in img[row, first_pix:]])) |
87 | | - return dac_out, img_width, img_height |
88 | | - |
89 | | - |
90 | | -def write_wav(filename, data, framerate): |
91 | | - """Create one channel 16bit wav file.""" |
92 | | - wav_file = wave.open(filename, "w") |
93 | | - nchannels = 1 |
94 | | - sampwidth = 2 |
95 | | - nframes = len(data) |
96 | | - comptype = "NONE" |
97 | | - compname = "not compressed" |
98 | | - if verbose: |
99 | | - print("Writing wav file", filename, "at rate", framerate, |
100 | | - "with", nframes, "samples") |
101 | | - wav_file.setparams((nchannels, sampwidth, framerate, nframes, |
102 | | - comptype, compname)) |
103 | | - wav_file.writeframes(data) |
104 | | - wav_file.close() |
105 | | - |
106 | | - |
107 | | -def main(cmdlineargs): ### pylint: disable=too-many-branches |
108 | | - """main(args)""" |
109 | | - global debug, fps, movie_file, output_filename, replaceforsync ### pylint: disable=global-statement |
110 | | - global threshold, start_offset, verbose ### pylint: disable=global-statement |
111 | | - |
112 | | - try: |
113 | | - opts, args = getopt.getopt(cmdlineargs, |
114 | | - "f:hmo:rs:t:v", ["help", "output="]) |
115 | | - except getopt.GetoptError as err: |
116 | | - print(err, |
117 | | - file=sys.stderr) |
118 | | - usage(2) |
119 | | - for opt, arg in opts: |
120 | | - if opt == "-d": ### pylint counts these towards too-many-branches :( |
121 | | - debug = 1 |
122 | | - elif opt == "-f": |
123 | | - fps = int(arg) |
124 | | - elif opt in ("-h", "--help"): |
125 | | - usage(0) |
126 | | - elif opt == "-m": |
127 | | - movie_file = True |
128 | | - elif opt in ("-o", "--output"): |
129 | | - output_filename = arg |
130 | | - elif opt == "-r": |
131 | | - replaceforsync = True |
132 | | - elif opt == "-s": |
133 | | - start_offset = int(arg) |
134 | | - elif opt == "-t": |
135 | | - threshold = int(arg) |
136 | | - elif opt == "-v": |
137 | | - verbose = True |
138 | | - else: |
139 | | - print("Internal error: unhandled option", |
140 | | - file=sys.stderr) |
141 | | - sys.exit(3) |
142 | | - |
143 | | - dac_samples = array.array("h", []) |
144 | | - |
145 | | - ### Decide whether to replace first column with sync pulse |
146 | | - ### or add it as an additional column |
147 | | - first_pix = 1 if replaceforsync else 0 |
148 | | - |
149 | | - ### Read each frame, either |
150 | | - ### many single image filenames in args or |
151 | | - ### one or more video (animated gifs) (needs -m on command line) |
152 | | - dac_y_range = dac_top - dac_bottom |
153 | | - row_offset = 0 |
154 | | - for arg in args: |
155 | | - if verbose: |
156 | | - print("PROCESSING", arg) |
157 | | - if movie_file: |
158 | | - images = imageio.mimread(arg) |
159 | | - else: |
160 | | - images = [imageio.imread(arg)] |
161 | | - |
162 | | - for img in images: |
163 | | - img_output, width, height = image_to_dac(img, row_offset, |
164 | | - first_pix, dac_y_range) |
165 | | - dac_samples.extend(img_output) |
166 | | - row_offset += start_offset |
167 | | - |
168 | | - write_wav(output_filename, dac_samples, |
169 | | - (width + (1 - first_pix)) * height * fps) |
170 | | - |
171 | | - |
172 | | -if __name__ == "__main__": |
173 | | - main(sys.argv[1:]) |
| 1 | +# SPDX-FileCopyrightText: 2019 Kevin J. Walters for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +#!/usr/bin/python3 |
| 6 | + |
| 7 | +### pngtowav v1.0 |
| 8 | +"""Convert a list of png images to pseudo composite video in wav file form. |
| 9 | +
|
| 10 | +This is Python code not intended for running on a microcontroller board. |
| 11 | +""" |
| 12 | + |
| 13 | +### MIT License |
| 14 | + |
| 15 | +### Copyright (c) 2019 Kevin J. Walters |
| 16 | + |
| 17 | +### Permission is hereby granted, free of charge, to any person obtaining a copy |
| 18 | +### of this software and associated documentation files (the "Software"), to deal |
| 19 | +### in the Software without restriction, including without limitation the rights |
| 20 | +### to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 21 | +### copies of the Software, and to permit persons to whom the Software is |
| 22 | +### furnished to do so, subject to the following conditions: |
| 23 | + |
| 24 | +### The above copyright notice and this permission notice shall be included in all |
| 25 | +### copies or substantial portions of the Software. |
| 26 | + |
| 27 | +### THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 28 | +### IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 29 | +### FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 30 | +### AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 31 | +### LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 32 | +### OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 33 | +### SOFTWARE. |
| 34 | + |
| 35 | +import getopt |
| 36 | +import sys |
| 37 | +import array |
| 38 | +import wave |
| 39 | + |
| 40 | +import imageio |
| 41 | + |
| 42 | + |
| 43 | +### globals |
| 44 | +### pylint: disable=invalid-name |
| 45 | +### start_offset of 1 can help if triggering on oscilloscope |
| 46 | +### is missing alternate lines |
| 47 | +debug = 0 |
| 48 | +verbose = False |
| 49 | +movie_file = False |
| 50 | +output_filename = "dacanim.wav" |
| 51 | +fps = 50 |
| 52 | +threshold = 128 ### pixel level |
| 53 | +replaceforsync = False |
| 54 | +start_offset = 1 |
| 55 | + |
| 56 | +max_dac_v = 3.3 |
| 57 | +### 16 bit wav files always use signed representation for data |
| 58 | +dac_offtop = 2**15-1 ### 3.30V |
| 59 | +dac_sync = -2**15 ### 0.00V |
| 60 | +### image from 3.00V to 0.30V |
| 61 | +dac_top = round(3.00 / max_dac_v * (2**16-1)) - 2**15 |
| 62 | +dac_bottom = round(0.30 / max_dac_v * (2**16-1)) - 2**15 |
| 63 | + |
| 64 | + |
| 65 | +def usage(exit_code): ### pylint: disable=missing-docstring |
| 66 | + print("pngtowav: " |
| 67 | + + "[-d] [-f fps] [-h] [-m] [-o outputfilename] [-r] [-s lineoffset] [-t threshold] [-v]", |
| 68 | + file=sys.stderr) |
| 69 | + if exit_code is not None: |
| 70 | + sys.exit(exit_code) |
| 71 | + |
| 72 | + |
| 73 | +def image_to_dac(img, row_offset, first_pix, dac_y_range): |
| 74 | + """Convert a single image to DAC output.""" |
| 75 | + dac_out = array.array("h", []) |
| 76 | + |
| 77 | + img_height, img_width = img.shape |
| 78 | + if verbose: |
| 79 | + print("W,H", img_width, img_height) |
| 80 | + |
| 81 | + for row_o in range(img_height): |
| 82 | + row = (row_o + row_offset) % img_height |
| 83 | + ### Currently using 0 to (n-1)/n range |
| 84 | + y_pos = round(dac_top - row / (img_height - 1) * dac_y_range) |
| 85 | + if verbose: |
| 86 | + print("Adding row", row, "at y_pos", y_pos) |
| 87 | + dac_out.extend(array.array("h", |
| 88 | + [dac_sync] |
| 89 | + + [y_pos if x >= threshold else dac_offtop |
| 90 | + for x in img[row, first_pix:]])) |
| 91 | + return dac_out, img_width, img_height |
| 92 | + |
| 93 | + |
| 94 | +def write_wav(filename, data, framerate): |
| 95 | + """Create one channel 16bit wav file.""" |
| 96 | + wav_file = wave.open(filename, "w") |
| 97 | + nchannels = 1 |
| 98 | + sampwidth = 2 |
| 99 | + nframes = len(data) |
| 100 | + comptype = "NONE" |
| 101 | + compname = "not compressed" |
| 102 | + if verbose: |
| 103 | + print("Writing wav file", filename, "at rate", framerate, |
| 104 | + "with", nframes, "samples") |
| 105 | + wav_file.setparams((nchannels, sampwidth, framerate, nframes, |
| 106 | + comptype, compname)) |
| 107 | + wav_file.writeframes(data) |
| 108 | + wav_file.close() |
| 109 | + |
| 110 | + |
| 111 | +def main(cmdlineargs): ### pylint: disable=too-many-branches |
| 112 | + """main(args)""" |
| 113 | + global debug, fps, movie_file, output_filename, replaceforsync ### pylint: disable=global-statement |
| 114 | + global threshold, start_offset, verbose ### pylint: disable=global-statement |
| 115 | + |
| 116 | + try: |
| 117 | + opts, args = getopt.getopt(cmdlineargs, |
| 118 | + "f:hmo:rs:t:v", ["help", "output="]) |
| 119 | + except getopt.GetoptError as err: |
| 120 | + print(err, |
| 121 | + file=sys.stderr) |
| 122 | + usage(2) |
| 123 | + for opt, arg in opts: |
| 124 | + if opt == "-d": ### pylint counts these towards too-many-branches :( |
| 125 | + debug = 1 |
| 126 | + elif opt == "-f": |
| 127 | + fps = int(arg) |
| 128 | + elif opt in ("-h", "--help"): |
| 129 | + usage(0) |
| 130 | + elif opt == "-m": |
| 131 | + movie_file = True |
| 132 | + elif opt in ("-o", "--output"): |
| 133 | + output_filename = arg |
| 134 | + elif opt == "-r": |
| 135 | + replaceforsync = True |
| 136 | + elif opt == "-s": |
| 137 | + start_offset = int(arg) |
| 138 | + elif opt == "-t": |
| 139 | + threshold = int(arg) |
| 140 | + elif opt == "-v": |
| 141 | + verbose = True |
| 142 | + else: |
| 143 | + print("Internal error: unhandled option", |
| 144 | + file=sys.stderr) |
| 145 | + sys.exit(3) |
| 146 | + |
| 147 | + dac_samples = array.array("h", []) |
| 148 | + |
| 149 | + ### Decide whether to replace first column with sync pulse |
| 150 | + ### or add it as an additional column |
| 151 | + first_pix = 1 if replaceforsync else 0 |
| 152 | + |
| 153 | + ### Read each frame, either |
| 154 | + ### many single image filenames in args or |
| 155 | + ### one or more video (animated gifs) (needs -m on command line) |
| 156 | + dac_y_range = dac_top - dac_bottom |
| 157 | + row_offset = 0 |
| 158 | + for arg in args: |
| 159 | + if verbose: |
| 160 | + print("PROCESSING", arg) |
| 161 | + if movie_file: |
| 162 | + images = imageio.mimread(arg) |
| 163 | + else: |
| 164 | + images = [imageio.imread(arg)] |
| 165 | + |
| 166 | + for img in images: |
| 167 | + img_output, width, height = image_to_dac(img, row_offset, |
| 168 | + first_pix, dac_y_range) |
| 169 | + dac_samples.extend(img_output) |
| 170 | + row_offset += start_offset |
| 171 | + |
| 172 | + write_wav(output_filename, dac_samples, |
| 173 | + (width + (1 - first_pix)) * height * fps) |
| 174 | + |
| 175 | + |
| 176 | +if __name__ == "__main__": |
| 177 | + main(sys.argv[1:]) |
0 commit comments