%pylab inline
from __future__ import print_function
from __future__ import division
Populating the interactive namespace from numpy and matplotlib
import wave
fp = wave.open("passport.wav", 'r')
print(fp)
<wave.Wave_read instance at 0x7fb094040cb0>
print(fp.getframerate(), fp.getnchannels(), fp.getnframes(), fp.getsampwidth(), fp.getcomptype(), fp.getcompname())
44100 1 331098 2 NONE not compressed
fp.getparams()
(1, 2, 44100, 331098, 'NONE', 'not compressed')
bytes = fp.readframes(8)
print(bytes)
Y � � � � � ����
for byte in bytes:
print(ord(byte))
89 0 225 0 182 0 137 0 188 0 129 0 252 255 179 255
You need to use the struct python module to pack/unpack bytes...
from scipy.io import wavfile
sr, samples = wavfile.read('passport.wav')
sr
44100
len(samples)
331098
type(samples)
numpy.ndarray
samples.dtype
dtype('int16')
max(samples), min(samples)
(22542, -21853)
plot(samples)
[<matplotlib.lines.Line2D at 0x7fb086f78a10>]
plot(samples/(2**15 -1)) # python3!
[<matplotlib.lines.Line2D at 0x7fb086c3fe90>]
plot(samples//(2**15 -1)) # python2 !!!!!
[<matplotlib.lines.Line2D at 0x7fb086b7b550>]
img = imread('python.jpeg')
imshow(img)
<matplotlib.image.AxesImage at 0x7fb086a56810>
img.dtype
dtype('uint8')
img.shape
(230, 219, 3)
subplot(131)
imshow(img[:,:,0])
colorbar()
subplot(132)
imshow(img[:,:,1])
colorbar()
subplot(133)
imshow(img[:,:,2])
colorbar()
gcf().set_figwidth(14)
subplot(131)
imshow(img[:,:-30,0], cmap=cm.gray)
colorbar()
subplot(132)
imshow(img[:,:-30,1], cmap=cm.gray)
colorbar()
subplot(133)
imshow(img[:,:-30,2], cmap=cm.gray)
colorbar()
gcf().set_figwidth(14)
imshow(img[:,:,0] + img[:,:,1], cmap=cm.gray)
colorbar()
<matplotlib.colorbar.Colorbar instance at 0x7fb08b599cf8>
imshow(img[:,:,0]/2 + img[:,:,1]/2, cmap=cm.gray)
colorbar()
<matplotlib.colorbar.Colorbar instance at 0x7fb0855e37e8>
imshow(img[:,:,0].astype(int16) + img[:,:,1].astype(int16), cmap=cm.gray)
colorbar()
<matplotlib.colorbar.Colorbar instance at 0x7fb0854459e0>
imshow(sum(img, axis=2), cmap=cm.gray)
colorbar()
<matplotlib.colorbar.Colorbar instance at 0x7fb08b6993b0>
sum(img, axis=2).dtype
dtype('uint64')
red_only = img.copy()
red_only[:,:,1] = 0
red_only[:,:,2] = 0
imshow(red_only)
<matplotlib.image.AxesImage at 0x7fb0861c2990>
green_only = img.copy()
green_only[:,:,0] = 0
green_only[:,:,2] = 0
imshow(green_only)
<matplotlib.image.AxesImage at 0x7fb086260c10>
sig = img[120,:, 1]
plot(sig)
sig.dtype
dtype('uint8')
sigout = list((sig - 100) * 200)*30
plot(sigout)
[<matplotlib.lines.Line2D at 0x7fb086103850>]
What?!?!
sig.dtype
dtype('uint8')
sigout = list((sig.astype(int16) - 100) * 200)*30
plot(sigout)
[<matplotlib.lines.Line2D at 0x7fb086023790>]
wavfile.write('outsig.wav', 44100, array(sigout, dtype=int16))
!aplay outsig.wav
Playing WAVE 'outsig.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Mono
a = array([[1,2,3],[4,5,6]])
b = array(a.flat)
b
array([1, 2, 3, 4, 5, 6])
audioout = array(((img[:,:,0].astype(int16)- 100) * 200).flat)
wavfile.write('outsig2.wav', 44100, audioout)
!aplay outsig2.wav
Playing WAVE 'outsig2.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Mono
audioout = array(((img[:,:,0].astype(int16)- 100) * 200).flat)
wavfile.write('outsig3.wav', 8000, audioout)
!aplay outsig3.wav
Playing WAVE 'outsig3.wav' : Signed 16 bit Little Endian, Rate 8000 Hz, Mono
sr,sample = wavfile.read('passport.wav')
sample.size
331098
256 * 256 * 3
196608
newimg = sample[:196608].reshape(256,256,3)
newimg.max(), (newimg/100.0).max()
(22542, 225.41999999999999)
newimg.shape
(256, 256, 3)
plot(sample)
[<matplotlib.lines.Line2D at 0x7fb0861f5c10>]
imshow(newimg/100.0)
<matplotlib.image.AxesImage at 0x7fb0858dd4d0>
imshow(100 + newimg/200.0)
<matplotlib.image.AxesImage at 0x7fb081a8b7d0>
256*512
131072
newimg = sample[:256*512].reshape(256,512)
imshow(newimg, cmap=cm.gray)
colorbar()
/usr/lib/python2.7/dist-packages/matplotlib/transforms.py:2649: RuntimeWarning: overflow encountered in short_scalars if vmax - vmin <= max(abs(vmin), abs(vmax)) * tiny:
<matplotlib.colorbar.Colorbar instance at 0x7fb08b4712d8>
imshow(abs(newimg), cmap=cm.gray)
colorbar()
<matplotlib.colorbar.Colorbar instance at 0x7fb0860b2cf8>
imsave('outimg.jpg', newimg, cmap=cm.cool)
More colormaps:
http://matplotlib.org/examples/color/colormaps_reference.html
newimgR = sample[:256*512].reshape(256,512, 1)
newimgG = sample[100:256*512 + 100].reshape(256,512, 1)
newimgB = sample[200:256*512 + 200].reshape(256,512, 1)
newimg = concatenate( [newimgR/2.0**8, newimgG/2.0**8, newimgB/2.0**8], axis = 2)
print(newimg.shape, newimg.dtype)
(256, 512, 3) float64
newimg.max(), newimg.min()
(88.0546875, -85.36328125)
imshow((newimg + 127.0).astype(uint8))
<matplotlib.image.AxesImage at 0x7fb085359b10>
imshow(newimg[:,:,0])
<matplotlib.image.AxesImage at 0x7fb0841d2950>
imshow(newimg[:,:,1])
<matplotlib.image.AxesImage at 0x7fb08417aed0>
imshow((newimg + 127).astype(float)/100)
colorbar()
<matplotlib.colorbar.Colorbar instance at 0x7fb081c7ef80>
imshow(newimg/100)
<matplotlib.image.AxesImage at 0x7fb081bc6f10>
newimgR = sample[:256*512].reshape(256,512, 1)
newimgG = sample[14000:256*512 + 14000
].reshape(256,512, 1)
newimgB = sample[10000:256*512 + 10000].reshape(256,512, 1)
newimg = concatenate( [newimgR/2.0**8, newimgG/2.0**8, newimgB/2.0**8], axis = 2)
print(newimg.shape)
(256, 512, 3)
imshow((newimg + 127).astype(uint8))
ylim((0,100))
xlim((100, 200))
(100, 200)
newimgR = sample[:256*512].reshape(256,512, 1)
newimgG = sample[14000:256*512 + 14000
].reshape(256,512, 1)
newimgB = sample[10000:256*512 + 10000].reshape(256,512, 1)
newimg = concatenate( [newimgR/2.0**13, newimgG/2.0**12, newimgB/2.0**8], axis = 2)
imshow((newimg + 127).astype(uint8))
ylim((0,100))
xlim((100, 200))
(100, 200)