Hide images in sound

spectrogram.py 907B

123456789101112131415161718192021222324252627282930313233
  1. #!/usr/bin/python
  2. import numpy as np
  3. import matplotlib.image as mpimg
  4. import wave
  5. from array import array
  6. def make_wav(image_filename):
  7. """ Make a WAV file having a spectrogram resembling an image """
  8. # Load image
  9. image = mpimg.imread(image_filename)
  10. image = np.sum(image, axis = 2).T[:, ::-1]
  11. image = image**3 # ???
  12. w, h = image.shape
  13. # Fourier transform, normalize, remove DC bias
  14. data = np.fft.irfft(image, h*2, axis=1).reshape((w*h*2))
  15. data -= np.average(data)
  16. data *= (2**15-1.)/np.amax(data)
  17. data = array("h", np.int_(data)).tostring()
  18. # Write to disk
  19. output_file = wave.open(image_filename+".wav", "w")
  20. output_file.setparams((1, 2, 44100, 0, "NONE", "not compressed"))
  21. output_file.writeframes(data)
  22. output_file.close()
  23. print "Wrote %s.wav" % image_filename
  24. if __name__ == "__main__":
  25. import sys
  26. make_wav(sys.argv[1])