Demo entry 6634609

hpss_image_convert

   

Submitted by Fraunhofer on Aug 11, 2017 at 16:01
Language: Python 3. Code size: 1.2 kB.

import numpy as np
from PIL import Image
from scipy import signal
from scipy.io import wavfile
from hpss import HPSS

fs = 44100
N = 2**11
H = N/2
w = signal.get_window('hann', N)

# open image
print("reading image file...")
im = Image.open("DSC_2736.jpg")
im.thumbnail((9999, H+1))

im_a = np.array(im)
if im_a.ndim>2: # convert to greyscale if necessary
    print("converting to greyscale...")
    im_a = np.round(im_a.mean(axis=2))

# flip image
im_a = np.flip(im_a, axis=0)

# convert to audio
print("converting image to audio...")
out = signal.istft(im_a, fs, w, N, N-H)[1]
print("saving...")
wavfile.write("output.wav", fs, out.astype(np.int16))

user_input = raw_input("Apply HPSS analysis (y/n): ")
if user_input != 'y':
     exit(0)

# separate "harmonic" from percussive components
print("applying HPSS transformation...")
X_im_h, X_im_p = HPSS(out, N, H, w, fs, 2, 2000)
im_h = signal.istft(X_im_h, fs, w, N, N-H)[1]
im_p = signal.istft(X_im_p, fs, w, N, N-H)[1]

# write audio file
print("saving...")
wavfile.write("output_h.wav", fs, im_h.astype(np.int16))
wavfile.write("output_p.wav", fs, im_p.astype(np.int16))

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).