def __init__(self, image_width, image_height, fft_size, color_scheme): self.image_width = image_width self.image_height = image_height self.fft_size = fft_size self.image = Image.new("RGB", (image_height, image_width)) self.palette = interpolate_colors( COLOR_SCHEMES.get( color_scheme, COLOR_SCHEMES[DEFAULT_COLOR_SCHEME_KEY])['spec_colors']) # generate the lookup which translates y-coordinate to fft-bin self.y_to_bin = [] f_min = 100.0 f_max = 22050.0 y_min = math.log10(f_min) y_max = math.log10(f_max) for y in range(self.image_height): freq = math.pow(10.0, y_min + y / (image_height - 1.0) * (y_max - y_min)) bin = freq / 22050.0 * (self.fft_size / 2 + 1) if bin < self.fft_size / 2: alpha = bin - int(bin) self.y_to_bin.append((int(bin), alpha * 255)) # this is a bit strange, but using image.load()[x,y] = ... is # a lot slower than using image.putadata and then rotating the image # so we store all the pixels in an array and then create the image when saving self.pixels = []
def __init__(self, image_width, image_height, color_scheme): if image_height % 2 == 0: print("WARNING: Height is not uneven, images look much better at uneven height") waveform_colors = COLOR_SCHEMES.get(color_scheme, COLOR_SCHEMES[DEFAULT_COLOR_SCHEME_KEY])['wave_colors'] background_color = waveform_colors[0] colors = waveform_colors[1:] self.image = Image.new("RGB", (image_width, image_height), background_color) self.image_width = image_width self.image_height = image_height self.draw = ImageDraw.Draw(self.image) self.previous_x, self.previous_y = None, None self.color_lookup = interpolate_colors(colors) self.pix = self.image.load()