]> git.parisson.com Git - timeside.git/commitdiff
fix dc and mean_level analyzers, add watermarking to graphers
authoryomguy <yomguy@parisson.com>
Wed, 11 May 2011 23:17:08 +0000 (23:17 +0000)
committeryomguy <yomguy@parisson.com>
Wed, 11 May 2011 23:17:08 +0000 (23:17 +0000)
timeside/analyzer/dc.py
timeside/analyzer/mean_level.py
timeside/grapher/core.py
timeside/grapher/spectrogram.py
timeside/grapher/waveform.py
timeside/grapher/waveform_awdio.py
timeside/grapher/waveform_joydiv.py

index d293d76cc195bc1728a031fb4fe2b078d49e8415..5b4ef04dcff38d7e5723f267987534c4a6a33407 100644 (file)
@@ -30,7 +30,7 @@ class MeanDCShift(Processor):
     @interfacedoc
     def setup(self, channels=None, samplerate=None, nframes=None):
         super(MeanDCShift, self).setup(channels, samplerate, nframes)
-        self.value = 0
+        self.values = numpy.array([0])
 
     @staticmethod
     @interfacedoc
@@ -51,9 +51,9 @@ class MeanDCShift(Processor):
         return "%s %s" % (str(self.value), unit())
 
     def process(self, frames, eod=False):
-        self.value = numpy.round(100*numpy.mean(frames),3)
+        self.values = numpy.append(self.values, numpy.mean(frames))
         return frames, eod
 
     def result(self):
-        return self.value
+        return numpy.round(100*numpy.mean(self.values),3)
 
index 6d47b86af88577b25e43527600be3fda52e3df79..4cdd0d687826889b97e54ba3fc128dcdb2452d8d 100644 (file)
@@ -31,7 +31,7 @@ class MeanLevel(Processor):
     @interfacedoc
     def setup(self, channels=None, samplerate=None, nframes=None):
         super(MeanLevel, self).setup(channels, samplerate, nframes)
-        self.value = -140
+        self.values = numpy.array([0])
 
     @staticmethod
     @interfacedoc
@@ -52,10 +52,8 @@ class MeanLevel(Processor):
         return "%s %s" % (str(self.value), unit())
 
     def process(self, frames, eod=False):
-        value = numpy.round(20*numpy.log10(numpy.mean(numpy.sqrt(numpy.square(frames)))), 2)
-        if value > self.value:
-            self.value = value
+        self.values = numpy.append(self.values, numpy.mean(numpy.sqrt(numpy.square(frames))))
         return frames, eod
 
     def result(self):
-        return self.value
+        return numpy.round(20*numpy.log10(numpy.mean(self.values)), 2)
index e79ecb719f7e068ed8d3bc3b2fd91ddd25838e99..7cb954aeab59caf5f49c5c23b15d09b4018ab5ec 100644 (file)
@@ -24,7 +24,7 @@
 
 
 import optparse, math, sys
-import ImageFilter, ImageChops, Image, ImageDraw, ImageColor
+import ImageFilter, ImageChops, Image, ImageDraw, ImageColor, ImageEnhance
 import numpy
 from timeside.core import FixedSizeInputAdapter
 
@@ -245,6 +245,9 @@ class WaveformImage(object):
                     self.draw_peaks(self.pixel_cursor, peaks, spectral_centroid)
                     self.pixel_cursor += 1
 
+    def watermark(self, text, color=None, opacity=.6, margin=(10,10)):
+        self.image = im_watermark(self.image, text, color=color, opacity=opacity, margin=margin)
+        
     def save(self, filename):
         """ Apply last 2D transforms and write all pixels to the file. """
 
@@ -358,6 +361,9 @@ class WaveformImageJoyContour(WaveformImage):
         if eod:
             self.draw_peaks_contour()
 
+    def watermark(self, text, color=None, opacity=.6, margin=(10,10)):
+        self.image = im_watermark(self.image, text, color=color, opacity=opacity, margin=margin)
+    
     def save(self, filename):
         """ Apply last 2D transforms and write all pixels to the file. """
         # middle line (0 for none)
@@ -456,6 +462,9 @@ class WaveformImageSimple(object):
                 self.draw_peaks(self.pixel_cursor, (0, 0))
                 self.pixel_cursor += 1
 
+    def watermark(self, text, color=None, opacity=.6, margin=(10,10)):
+        self.image = im_watermark(self.image, text, color=color, opacity=opacity, margin=margin)
+        
     def save(self, filename):
         """ Apply last 2D transforms and write all pixels to the file. """
         
@@ -536,6 +545,9 @@ class SpectrogramImage(object):
                     (spectral_centroid, db_spectrum) = self.spectrum.process(samples, True)
                     self.draw_spectrum(self.pixel_cursor, db_spectrum)
                     self.pixel_cursor += 1
+    
+    def watermark(self, text, color=None, opacity=.6, margin=(10,10)):
+        self.image = im_watermark(self.image, text, color=color, opacity=opacity, margin=margin)
 
     def save(self, filename):
         """ Apply last 2D transforms and write all pixels to the file. """
@@ -648,3 +660,29 @@ def smooth(x, window_len=10, window='hanning'):
     y = numpy.convolve(w/w.sum(), s, mode='same')
     return y[window_len-1:-window_len+1]
 
+def reduce_opacity(im, opacity):
+    """Returns an image with reduced opacity."""
+    assert opacity >= 0 and opacity <= 1
+    if im.mode != 'RGBA':
+        im = im.convert('RGBA')
+    else:
+        im = im.copy()
+    alpha = im.split()[3]
+    alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
+    im.putalpha(alpha)
+    return im
+
+def im_watermark(im, inputtext, font=None, color=None, opacity=.6, margin=(30,30)):
+    """
+    imprints a PIL image with the indicated text in lower-right corner
+    """
+    if im.mode != "RGBA":
+        im = im.convert("RGBA")
+    textlayer = Image.new("RGBA", im.size, (0,0,0,0))
+    textdraw = ImageDraw.Draw(textlayer)
+    textsize = textdraw.textsize(inputtext, font=font)
+    textpos = [im.size[i]-textsize[i]-margin[i] for i in [0,1]]
+    textdraw.text(textpos, inputtext, font=font, fill=color)
+    if opacity != 1:
+        textlayer = reduce_opacity(textlayer,opacity)
+    return Image.composite(textlayer, im, textlayer)
index 0d98a0d1b59da0a696386cfa767b163cb7afcf4d..fa80a92b9a0afcda3b30e8cc08d9bdd5112cafb0 100644 (file)
@@ -68,4 +68,7 @@ class Spectrogram(Processor):
         if output:
             self.graph.save(output)
         return self.graph.image
-        
+    
+    def watermark(self, text, font=None, color=(255, 255, 255), opacity=.6, margin=(5,5)):
+        self.graph.watermark(text, color=color, opacity=opacity, margin=margin)
+
index 3077d093e9265fedaec03a8c36629a080ba94900..7249f46f6165f40e0bcbaf4a5bda8d956ebd02f4 100644 (file)
@@ -62,10 +62,12 @@ class Waveform(Processor):
     def process(self, frames, eod=False):
         self.graph.process(frames, eod)
         return frames, eod
-
+        
     @interfacedoc
     def render(self, output=None):
         if output:
             self.graph.save(output)
         return self.graph.image
         
+    def watermark(self, text, font=None, color=(255, 255, 255), opacity=.6, margin=(5,5)):
+        self.graph.watermark(text, color=color, opacity=opacity, margin=margin)
index 49c848fa43a3f3aa80750fed98d74f845db9da45..26eae20795a22e071b278e8583e70527af234d83 100644 (file)
@@ -68,3 +68,5 @@ class WaveformAwdio(Processor):
             self.graph.save(output)
         return self.graph.image
         
+    def watermark(self, text, font=None, color=(255, 255, 255), opacity=.6, margin=(5,5)):
+        self.graph.watermark(text, color=color, opacity=opacity, margin=margin)
index 526541cbb65af0f00ee5a5d6924ef0baeac24de3..9af9828bc3a95d833a05a393d4d2108e6d198577 100644 (file)
@@ -57,8 +57,6 @@ class WaveformJoyDiv(Processor):
     @interfacedoc
     def setup(self, channels=None, samplerate=None, nframes=None):
         super(WaveformJoyDiv, self).setup(channels, samplerate, nframes)
-#        if self.graph:
-#            self.graph = None
         self.graph = WaveformImageJoyContour(self.width, self.height, self.nframes(), self.samplerate(), self.FFT_SIZE,
                                     bg_color=self.bg_color, color_scheme=self.color_scheme,  ndiv=self.ndiv, symetry=self.symetry)
 
@@ -75,3 +73,6 @@ class WaveformJoyDiv(Processor):
 
     def release(self):
         self.graph.release()
+
+    def watermark(self, text, font=None, color=(255, 255, 255), opacity=.6, margin=(5,5)):
+        self.graph.watermark(text, color=color, opacity=opacity, margin=margin)