From c2803e565c3989df8231c5f076c3dee28312238a Mon Sep 17 00:00:00 2001 From: olivier <> Date: Wed, 24 Dec 2008 18:54:06 +0000 Subject: [PATCH] allow visualization colors customization --- telemeta/visualization/api.py | 7 +++- telemeta/visualization/spectrogram.py | 3 ++ telemeta/visualization/spectrogram2.py | 5 ++- telemeta/visualization/spectrogram3.py | 10 ++++- telemeta/visualization/spectrogram4.py | 10 ++++- telemeta/visualization/wav2png.py | 57 ++++++++++++++++---------- telemeta/visualization/waveform.py | 3 ++ telemeta/visualization/waveform2.py | 3 ++ telemeta/visualization/waveform3.py | 10 ++++- telemeta/visualization/waveform4.py | 11 ++++- 10 files changed, 90 insertions(+), 29 deletions(-) diff --git a/telemeta/visualization/api.py b/telemeta/visualization/api.py index 96727f2c..d68fc53e 100644 --- a/telemeta/visualization/api.py +++ b/telemeta/visualization/api.py @@ -20,5 +20,10 @@ class IMediaItemVisualizer(Interface): etc.. """ + def set_colors(self, background=None, scheme=None): + """Set the colors used for image generation. background is a RGB tuple, + and scheme a a predefined color theme name""" + pass + def render(media_item, width=None, height=None, options=None): - """Generator that streams the visualization output as a PNG image""" \ No newline at end of file + """Generator that streams the visualization output as a PNG image""" diff --git a/telemeta/visualization/spectrogram.py b/telemeta/visualization/spectrogram.py index e24cefa5..f4f0bfc3 100644 --- a/telemeta/visualization/spectrogram.py +++ b/telemeta/visualization/spectrogram.py @@ -25,6 +25,9 @@ class SpectrogramVisualizer(SnackCoreVisualizer): def get_name(self): return "Spectrogram 1" + def set_colors(self, background=None, scheme=None): + pass + def render(self, media_item, options=None): """Generator that streams the spectral view as a PNG image""" diff --git a/telemeta/visualization/spectrogram2.py b/telemeta/visualization/spectrogram2.py index ea0663d2..d4c41e98 100644 --- a/telemeta/visualization/spectrogram2.py +++ b/telemeta/visualization/spectrogram2.py @@ -25,8 +25,11 @@ class SpectrogramVisualizer2(OctaveCoreVisualizer): def get_name(self): return "Spectrogram (octave)" + + def set_colors(self, background=None, scheme=None): + pass - def render(self, media_item, options=None): + def render(self, media_item, width=None, height=None, options=None): """Generator that streams the spectral view as a PNG image""" stream = self.octave_to_png_stream(media_item) diff --git a/telemeta/visualization/spectrogram3.py b/telemeta/visualization/spectrogram3.py index 340d2c36..f970e470 100644 --- a/telemeta/visualization/spectrogram3.py +++ b/telemeta/visualization/spectrogram3.py @@ -18,12 +18,19 @@ class SpectrogramVisualizer3(Component): implements(IMediaItemVisualizer) + bg_color = None + color_scheme = None + def get_id(self): return "spectrogram3" def get_name(self): return "Spectrogram (audiolab)" + def set_colors(self, background=None, scheme=None): + self.bg_color = background + self.color_scheme = scheme + def render(self, media_item, width=None, height=None, options=None): """Generator that streams the spectrogram as a PNG image with a python method""" @@ -40,7 +47,8 @@ class SpectrogramVisualizer3(Component): image_height = 150 fft_size = 2048 - args = (wav_file, pngFile.name, image_width, image_height, fft_size) + args = (wav_file, pngFile.name, image_width, image_height, fft_size, + self.bg_color, self.color_scheme) create_spectrogram_png(*args) buffer = pngFile.read(0xFFFF) diff --git a/telemeta/visualization/spectrogram4.py b/telemeta/visualization/spectrogram4.py index 9c9e7efc..87e68740 100644 --- a/telemeta/visualization/spectrogram4.py +++ b/telemeta/visualization/spectrogram4.py @@ -18,12 +18,19 @@ class SpectrogramVisualizer3(Component): implements(IMediaItemVisualizer) + bg_color = None + color_scheme = None + def get_id(self): return "spectrogram4" def get_name(self): return "Spectrogram (audiolab large)" + def set_colors(self, background=None, scheme=None): + self.bg_color = background + self.color_scheme = scheme + def render(self, media_item, width=None, height=None, options=None): """Generator that streams the spectrogram as a PNG image with a python method""" @@ -40,7 +47,8 @@ class SpectrogramVisualizer3(Component): image_height = 300 fft_size = 2048 - args = (wav_file, pngFile.name, image_width, image_height, fft_size) + args = (wav_file, pngFile.name, image_width, image_height, fft_size, + self.bg_color, self.color_scheme) create_spectrogram_png(*args) buffer = pngFile.read(0xFFFF) diff --git a/telemeta/visualization/wav2png.py b/telemeta/visualization/wav2png.py index fee6809c..adf2dcb5 100755 --- a/telemeta/visualization/wav2png.py +++ b/telemeta/visualization/wav2png.py @@ -28,6 +28,19 @@ import ImageFilter, ImageChops, Image, ImageDraw, ImageColor import numpy import scikits.audiolab as audiolab +color_schemes = { + 'default': { + 'waveform': [(50,0,200), (0,220,80), (255,224,0), (255,0,0)], + 'spectrogram': [(0, 0, 0), (58/4,68/4,65/4), (80/2,100/2,153/2), (90,180,100), + (224,224,44), (255,60,30), (255,255,255)] + }, + 'purple': { + 'waveform': [(173,173,173), (147,149,196), (77,80,138), (108,66,0)], + 'spectrogram': [(0, 0, 0), (58/4,68/4,65/4), (80/2,100/2,153/2), (90,180,100), + (224,224,44), (255,60,30), (255,255,255)] + } +} + class TestAudioFile(object): """A class that mimics audiolab.sndfile but generates noise instead of reading a wave file. Additionally it can be told to have a "broken" header and thus crashing @@ -244,8 +257,13 @@ def interpolate_colors(colors, flat=False, num_colors=256): class WaveformImage(object): - def __init__(self, image_width, image_height): - self.image = Image.new("RGB", (image_width, image_height)) + def __init__(self, image_width, image_height, bg_color = None, color_scheme = None): + if not bg_color: + bg_color = (0,0,0) + if not color_scheme: + color_scheme = 'default' + + self.image = Image.new("RGB", (image_width, image_height), bg_color) self.image_width = image_width self.image_height = image_height @@ -253,12 +271,7 @@ class WaveformImage(object): self.draw = ImageDraw.Draw(self.image) self.previous_x, self.previous_y = None, None - colors = [ - (50,0,200), - (0,220,80), - (255,224,0), - (255,0,0), - ] + colors = color_schemes[color_scheme]['waveform'] # this line gets the old "screaming" colors back... # colors = [self.color_from_value(value/29.0) for value in range(0,30)] @@ -327,22 +340,20 @@ class WaveformImage(object): class SpectrogramImage(object): - def __init__(self, image_width, image_height, fft_size): + def __init__(self, image_width, image_height, fft_size, bg_color = None, color_scheme = None): + + #FIXME: bg_color is ignored + + if not color_scheme: + color_scheme = 'default' + self.image = Image.new("P", (image_height, image_width)) self.image_width = image_width self.image_height = image_height self.fft_size = fft_size - colors = [ - (0, 0, 0), - (58/4,68/4,65/4), - (80/2,100/2,153/2), - (90,180,100), - (224,224,44), - (255,60,30), - (255,255,255) - ] + colors = color_schemes[color_scheme]['spectrogram'] self.image.putpalette(interpolate_colors(colors, True)) @@ -378,13 +389,14 @@ class SpectrogramImage(object): self.image.transpose(Image.ROTATE_90).save(filename) -def create_wavform_png(input_filename, output_filename_w, image_width, image_height, fft_size): +def create_wavform_png(input_filename, output_filename_w, image_width, image_height, fft_size, + bg_color = None, color_scheme = None): audio_file = audiolab.sndfile(input_filename, 'read') samples_per_pixel = audio_file.get_nframes() / float(image_width) processor = AudioProcessor(audio_file, fft_size, numpy.hanning) - waveform = WaveformImage(image_width, image_height) + waveform = WaveformImage(image_width, image_height, bg_color, color_scheme) for x in range(image_width): @@ -404,13 +416,14 @@ def create_wavform_png(input_filename, output_filename_w, image_width, image_hei print " done" -def create_spectrogram_png(input_filename, output_filename_s, image_width, image_height, fft_size): +def create_spectrogram_png(input_filename, output_filename_s, image_width, image_height, fft_size, + bg_color = None, color_scheme = None): audio_file = audiolab.sndfile(input_filename, 'read') samples_per_pixel = audio_file.get_nframes() / float(image_width) processor = AudioProcessor(audio_file, fft_size, numpy.hanning) - spectrogram = SpectrogramImage(image_width, image_height, fft_size) + spectrogram = SpectrogramImage(image_width, image_height, fft_size, bg_color, color_scheme) for x in range(image_width): diff --git a/telemeta/visualization/waveform.py b/telemeta/visualization/waveform.py index 6959cc40..5868c23c 100644 --- a/telemeta/visualization/waveform.py +++ b/telemeta/visualization/waveform.py @@ -28,6 +28,9 @@ class WaveFormVisualizer(Component): def get_name(self): return "Waveform" + def set_colors(self, background=None, scheme=None): + pass + def render(self, media_item, options=None): """Generator that streams the waveform as a PNG image""" diff --git a/telemeta/visualization/waveform2.py b/telemeta/visualization/waveform2.py index 18567e04..5c4ff9a1 100644 --- a/telemeta/visualization/waveform2.py +++ b/telemeta/visualization/waveform2.py @@ -28,6 +28,9 @@ class WaveformVisualizer2(OctaveCoreVisualizer): def get_name(self): return "Waveform (octave)" + def set_colors(self, background=None, scheme=None): + pass + def render(self, media_item, options=None): """Generator that streams the temporal view as a PNG image""" diff --git a/telemeta/visualization/waveform3.py b/telemeta/visualization/waveform3.py index 55644f13..f6a111e6 100644 --- a/telemeta/visualization/waveform3.py +++ b/telemeta/visualization/waveform3.py @@ -18,12 +18,19 @@ class WaveFormVisualizer(Component): implements(IMediaItemVisualizer) + bg_color = None + color_scheme = None + def get_id(self): return "waveform3" def get_name(self): return "Waveform (audiolab)" + def set_colors(self, background=None, scheme=None): + self.bg_color = background + self.color_scheme = scheme + def render(self, media_item, width=None, height=None, options=None): """Generator that streams the waveform as a PNG image with a python method""" @@ -40,7 +47,8 @@ class WaveFormVisualizer(Component): image_height = 150 fft_size = 2048 - args = (wav_file, pngFile.name, image_width, image_height, fft_size) + args = (wav_file, pngFile.name, image_width, image_height, fft_size, + self.bg_color, self.color_scheme) create_wavform_png(*args) buffer = pngFile.read(0xFFFF) diff --git a/telemeta/visualization/waveform4.py b/telemeta/visualization/waveform4.py index 10895304..93d9b57f 100644 --- a/telemeta/visualization/waveform4.py +++ b/telemeta/visualization/waveform4.py @@ -18,13 +18,20 @@ class WaveFormVisualizer(Component): implements(IMediaItemVisualizer) + bg_color = None + color_scheme = None + def get_id(self): return "waveform4" def get_name(self): return "Waveform (audiolab large)" - def render(self, media_item, width=None, height=None, options=None): + def set_colors(self, background=None, scheme=None): + self.bg_color = background + self.color_scheme = scheme + + def render(self, media_item, width=None, height=None, options=None): """Generator that streams the waveform as a PNG image with a python method""" wav_file = media_item.file.path @@ -40,7 +47,7 @@ class WaveFormVisualizer(Component): image_height = 300 fft_size = 2048 - args = (wav_file, pngFile.name, image_width, image_height, fft_size) + args = (wav_file, pngFile.name, image_width, image_height, fft_size, self.bg_color, self.color_scheme) create_wavform_png(*args) buffer = pngFile.read(0xFFFF) -- 2.39.5