%pylab inline from __future__ import print_function from __future__ import division import wave fp = wave.open("passport.wav", 'r') print(fp) print(fp.getframerate(), fp.getnchannels(), fp.getnframes(), fp.getsampwidth(), fp.getcomptype(), fp.getcompname()) fp.getparams() bytes = fp.readframes(8) print(bytes) for byte in bytes: print(ord(byte)) from scipy.io import wavfile sr, samples = wavfile.read('passport.wav') sr len(samples) type(samples) samples.dtype max(samples), min(samples) plot(samples) plot(samples/(2**15 -1)) # python3! plot(samples//(2**15 -1)) # python2 !!!!! img = imread('python.jpeg') imshow(img) img.dtype img.shape 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() imshow(img[:,:,0]/2 + img[:,:,1]/2, cmap=cm.gray) colorbar() imshow(img[:,:,0].astype(int16) + img[:,:,1].astype(int16), cmap=cm.gray) colorbar() imshow(sum(img, axis=2), cmap=cm.gray) colorbar() sum(img, axis=2).dtype red_only = img.copy() red_only[:,:,1] = 0 red_only[:,:,2] = 0 imshow(red_only) green_only = img.copy() green_only[:,:,0] = 0 green_only[:,:,2] = 0 imshow(green_only) sig = img[120,:, 1] plot(sig) sig.dtype sigout = list((sig - 100) * 200)*30 plot(sigout) sig.dtype sigout = list((sig.astype(int16) - 100) * 200)*30 plot(sigout) wavfile.write('outsig.wav', 44100, array(sigout, dtype=int16)) !aplay outsig.wav a = array([[1,2,3],[4,5,6]]) b = array(a.flat) b audioout = array(((img[:,:,0].astype(int16)- 100) * 200).flat) wavfile.write('outsig2.wav', 44100, audioout) !aplay outsig2.wav audioout = array(((img[:,:,0].astype(int16)- 100) * 200).flat) wavfile.write('outsig3.wav', 8000, audioout) !aplay outsig3.wav sr,sample = wavfile.read('passport.wav') sample.size 256 * 256 * 3 newimg = sample[:196608].reshape(256,256,3) newimg.max(), (newimg/100.0).max() newimg.shape plot(sample) imshow(newimg/100.0) imshow(100 + newimg/200.0) 256*512 newimg = sample[:256*512].reshape(256,512) imshow(newimg, cmap=cm.gray) colorbar() imshow(abs(newimg), cmap=cm.gray) colorbar() imsave('outimg.jpg', newimg, cmap=cm.cool) 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) newimg.max(), newimg.min() imshow((newimg + 127.0).astype(uint8)) imshow(newimg[:,:,0]) imshow(newimg[:,:,1]) imshow((newimg + 127).astype(float)/100) colorbar() imshow(newimg/100) 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) imshow((newimg + 127).astype(uint8)) ylim((0,100)) xlim((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))