From 2c739d64cc97d0b1f6d37f4fbe55c3c927c63a6a Mon Sep 17 00:00:00 2001 From: kira1752 Date: Tue, 25 Sep 2018 16:11:59 +0800 Subject: [PATCH] Change the name of sampling_rate variable to sample_rate for better clarity and update readme file to include usage help and usage recommendation --- README.md | 27 ++++++++++++++++++++++++++- simtsou.py | 34 +++++++++++++++++----------------- 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 3ca4047..e18298f 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,28 @@ # Simtsou -A simple image to sound conversion python script. \ No newline at end of file +A simple image to sound conversion python script. For now, this script only support output to .wav audio format only (which is lossless audio format). More lossless audio format will coming soon in future version. Its recommended to use lossless picture format for better quality output :) + +Usage help: + usage: simtsou.py [-h] [-r] [-i] [-o OUTPUT] [-b BOTTOM] [-t TOP] [-p PIXELS] + [-s SAMPLING] + INPUT_FILE + + positional arguments: + INPUT_FILE Name of the image to be converted. + + optional arguments: + -h, --help show this help message and exit + + -r, --rotate Rotate image 90 degrees. + + -i, --invert Invert image colors. + + -o OUTPUT, --output OUTPUT Name of the output wav file. Default value: out.wav). + + -b BOTTOM, --bottom BOTTOM Bottom frequency range. Default value: 200. + + -t TOP, --top TOP Top frequency range. Default value: 20000. + + -p PIXELS, --pixels PIXELS Pixels per second. Default value: 30. + + -s SAMPLING, --sampling SAMPLING Sample rate. Default value: 44100. diff --git a/simtsou.py b/simtsou.py index 044c6cf..7a86adb 100644 --- a/simtsou.py +++ b/simtsou.py @@ -15,20 +15,20 @@ def parser(): parser.add_argument ("-b", "--bottom", help="Bottom frequency range. Default value: 200.", type=int) parser.add_argument ("-t", "--top", help="Top frequency range. Default value: 20000.", type=int) parser.add_argument ("-p", "--pixels", help="Pixels per second. Default value: 30.", type=int) - parser.add_argument ("-s", "--sampling", help="Sampling rate. Default value: 44100.", type=int) + parser.add_argument ("-s", "--sampling", help="Sample rate. Default value: 44100.", type=int) # Create "arguments" object based on user input arguments = parser.parse_args() # Default values - min_freq = 200 - max_freq = 20000 - sampling_rate = 44100 - pixels = 30 - output = "out.wav" - rotate = False - invert = False + min_freq = 200 + max_freq = 20000 + sample_rate = 44100 + pixels = 30 + output = "out.wav" + rotate = False + invert = False # Check arguments values if arguments.output: @@ -40,7 +40,7 @@ def parser(): if arguments.pixels: pixels = arguments.pixels if arguments.sampling: - sampling_rate = arguments.sampling + sample_rate = arguments.sampling if arguments.rotate: rotate = True if arguments.invert: @@ -50,12 +50,12 @@ def parser(): print ('Input file: %s.' % arguments.INPUT_FILE) print ('Frequency range: %d - %d.' % (min_freq, max_freq)) print ('Pixels per second: %d.' % pixels) - print ('Sampling rate: %d.' % sampling_rate) + print ('Sampling rate: %d.' % sample_rate) print ('Rotate Image: %s.' % ('Yes' if rotate else 'No')) - return (arguments.INPUT_FILE, output, min_freq, max_freq, pixels, sampling_rate, rotate, invert) + return (arguments.INPUT_FILE, output, min_freq, max_freq, pixels, sample_rate, rotate, invert) -def convert (INPUT_FILE, output, min_freq, max_freq, pixels, sampling_rate, rotate, invert): +def convert (INPUT_FILE, output, min_freq, max_freq, pixels, sample_rate, rotate, invert): image = Image.open (INPUT_FILE).convert ('L') # rotate image if requested @@ -67,12 +67,12 @@ def convert (INPUT_FILE, output, min_freq, max_freq, pixels, sampling_rate, rota image = ImageOps.invert (image) output = wave.open (output, 'w') - output.setparams ((1, 2, sampling_rate, 0, 'NONE', 'not compressed')) + output.setparams ((1, 2, sample_rate, 0, 'NONE', 'not compressed')) freq_range = max_freq - min_freq interval = freq_range / image.size[1] - samples = sampling_rate // pixels + samples = sample_rate // pixels data = array.array ('h') # Converting process start @@ -84,7 +84,7 @@ def convert (INPUT_FILE, output, min_freq, max_freq, pixels, sampling_rate, rota yinv = image.size[1] - y - 1 amplitude = image.getpixel ((x,y)) if (amplitude > 0): - row.append (gen_wave (yinv * interval + min_freq, amplitude, samples, sampling_rate)) + row.append (gen_wave (yinv * interval + min_freq, amplitude, samples, sample_rate)) for i in range(samples): for j in row: @@ -110,8 +110,8 @@ def convert (INPUT_FILE, output, min_freq, max_freq, pixels, sampling_rate, rota print ("Conversion progress: 100%") print ("Success. Completed in %d seconds." % int(time_end - time_start)) -def gen_wave (frequency, amplitude, samples, sampling_rate): - cycles = samples * frequency / sampling_rate +def gen_wave (frequency, amplitude, samples, sample_rate): + cycles = samples * frequency / sample_rate wave = [] for i in range(samples): x = math.sin (float (cycles) * 2 * math.pi * i / float (samples)) * float (amplitude)