将图像加载进内存
import numpy as np
import matplotlib.pyplot as plt
N = 512
NSQUARES = 30
img = np.zeros((N, N), np.uint8)
centers = np.random.random_integers(0, N, size=(NSQUARES, 2))
radii = np.random.randint(0, N/9, size=NSQUARES)
colors = np.random.randint(100, 255, size=NSQUARES)
for i in xrange(NSQUARES):
xindices = range(centers[i][0] - radii[i], centers[i][0] + radii[i])
xindices = np.clip(xindices, 0, N - 1)
yindices = range(centers[i][1] - radii[i], centers[i][1] + radii[i])
yindices = np.clip(yindices, 0, N - 1)
if len(xindices) == 0 or len(yindices) == 0:
continue
coordinates = np.meshgrid(xindices, yindices)
img[coordinates] = colors[i]
img.tofile('random_squares.raw')
img_memmap = np.memmap('random_squares.raw', shape=img.shape)
plt.imshow(img_memmap)
plt.axis('off')
plt.show()
组合图像
import numpy as np import
matplotlib.pyplot as plt
from scipy.misc import lena
ITERATIONS = 10
lena = lena()
SIZE = lena.shape[0]
MAX_COLOR = 255.
x_min, x_max = -2.5, 1
y_min, y_max = -1, 1
x, y = np.meshgrid(np.linspace(x_min, x_max, SIZE),
np.linspace(y_min, y_max, SIZE))
c = x + 1j * y
z = c.copy()
fractal = np.zeros(z.shape, dtype=np.uint8) + MAX_COLOR
for n in range(ITERATIONS):
mask = np.abs(z) <= 4
z[mask] = z[mask] ** 2 + c[mask]
fractal[(fractal == MAX_COLOR) & (-mask)] = (MAX_COLOR - 1) * n / ITERATIONS
plt.subplot(211)
plt.imshow(fractal)
plt.title('Mandelbrot')
plt.axis('off')
plt.subplot(212)
plt.imshow(np.choose(fractal < lena, [fractal, lena]))
plt.axis('off')
plt.title('Mandelbrot + Lena')
plt.show()
使图像变模糊
import numpy as np
import matplotlib.pyplot as plt
from random import choice
import scipy
import scipy.ndimage
NFIGURES = 5
k = np.random.random_integers(1, 5, NFIGURES)
a = np.random.random_integers(1, 5, NFIGURES)
colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k']
lena = scipy.misc.lena()
plt.subplot(211)
plt.imshow(lena)
plt.axis('off')
plt.subplot(212)
blurred = scipy.ndimage.gaussian_filter(lena, sigma=4)
plt.imshow(blurred)
plt.axis('off')
theta = np.linspace(0, k[0] * np.pi, 200)
plt.polar(theta, np.sqrt(theta), choice(colors))
for i in xrange(1, NFIGURES):
theta = np.linspace(0, k[i] * np.pi, 200)
plt.polar(theta, a[i] * np.cos(k[i] * theta), choice(colors))
plt.axis('off')
plt.show()
重复声音片段
import scipy.io.wavfile
import matplotlib.pyplot as plt
import urllib2
import numpy as np
response = urllib2.urlopen('http://www.thesoundarchive.com/ austinpowers/smashingbaby.wav')
print(response.info())
WAV_FILE = 'smashingbaby.wav'
filehandle = open(WAV_FILE, 'w')
filehandle.write(response.read())
filehandle.close()
sample_rate, data = scipy.io.wavfile.read(WAV_FILE)
print("Data type", data.dtype, "Shape", data.shape)
plt.subplot(2, 1, 1)
plt.title("Original")
plt.plot(data)
plt.subplot(2, 1, 2)
repeated = np.tile(data, 3)
plt.title("Repeated")
plt.plot(repeated)
scipy.io.wavfile.write("repeated_yababy.wav", sample_rate, repeated)
plt.show()
生成声音
import scipy.io.wavfile
import numpy as np
import matplotlib.pyplot as plt
RATE = 44100
DTYPE = np.int16
def generate(freq, amp, duration, phi):
t = np.linspace(0, duration, duration * RATE)
data = np.sin(2 * np.pi * freq * t + phi) * amp
return data.astype(DTYPE)
NTONES = 89
amps = 2000. * np.random.random((NTONES,)) + 200.
durations = 0.19 * np.random.random((NTONES,)) + 0.01
keys = np.random.random_integers(1, 88, NTONES)
freqs = 440.0 * 2 ** ((keys - 49.)/12.)
phi = 2 * np.pi * np.random.random((NTONES,))
tone = np.array([], dtype=DTYPE)
for i in xrange(NTONES):
newtone = generate(freqs[i], amp=amps[i], duration=durations[i], phi=phi[i])
tone = np.concatenate((tone, newtone))
scipy.io.wavfile.write('generated_tone.wav', RATE, tone)
plt.plot(np.linspace(0, len(tone)/RATE, len(tone)), tone)
plt.show()
设计音频滤波器
import scipy.io.wavfile
import matplotlib.pyplot as plt
import urllib2
import numpy as np
response = urllib2.urlopen('http://www.thesoundarchive.com/ austinpowers/smashingbaby.wav')
print(response.info())
WAV_FILE = 'smashingbaby.wav'
filehandle = open(WAV_FILE, 'w')
filehandle.write(response.read())
filehandle.close()
sample_rate, data = scipy.io.wavfile.read(WAV_FILE)
print("Data type", data.dtype, "Shape", data.shape)
plt.subplot(2, 1, 1)
plt.title("Original")
plt.plot(data)
b,a = scipy.signal.iirdesign(wp=0.2, ws=0.1, gstop=60, gpass=1, ftype='butter')
filtered = scipy.signal.lfilter(b, a, data)
plt.subplot(2, 1, 2)
plt.title("Filtered")
plt.plot(filtered)
scipy.io.wavfile.write('filtered.wav', sample_rate, filtered. astype(data.dtype))
plt.show()
Sobel 过滤器的边界检测
import scipy
import scipy.ndimage
import matplotlib.pyplot as plt
lena = scipy.misc.lena()
plt.subplot(221)
plt.imshow(lena)
plt.title('Original')
plt.axis('off')
sobelx = scipy.ndimage.sobel(lena, axis=0, mode='constant')
plt.subplot(222)
plt.imshow(sobelx)
plt.title('Sobel X')
plt.axis('off')
sobely = scipy.ndimage.sobel(lena, axis=1, mode='constant')
plt.subplot(223)
plt.imshow(sobely)
plt.title('Sobel Y')
plt.axis('off')
default = scipy.ndimage.sobel(lena)
plt.subplot(224)
plt.imshow(default)
plt.title('Default Filter')
plt.axis('off')
plt.show()