调整图像尺寸
import scipy.misc
import matplotlib.pyplot as plt
import numpy as np
lena = scipy.misc.lena()
LENA_X = 512
LENA_Y = 512
np.testing.assert_equal((LENA_Y, LENA_X), lena.shape)
yfactor = 2
xfactor = 3
resized = lena.repeat(yfactor, axis=0)
.repeat(xfactor, axis=1)
np.testing.assert_equal((yfactor * LENA_Y, xfactor * LENA_Y), resized.shape)
plt.subplot(211)
plt.title("Lena")
plt.axis("off")
plt.imshow(lena)
plt.subplot(212)
plt.title("Resized")
plt.axis("off")
plt.imshow(resized)
plt.show()
创建视图及副本
import scipy.misc
import matplotlib.pyplot as plt
lena = scipy.misc.lena()
acopy = lena.copy()
aview = lena.view()
plt.subplot(221)
plt.imshow(lena)
plt.subplot(222)
plt.imshow(acopy)
plt.subplot(223)
plt.imshow(aview)
aview.flat = 0
plt.subplot(224)
plt.imshow(aview)
翻转图像
import scipy.misc
import matplotlib.pyplot as plt
lena = scipy.misc.lena()
plt.subplot(221)
plt.title('Original')
plt.axis('off')
plt.imshow(lena)
plt.subplot(222)
plt.title('Flipped')
plt.axis('off')
plt.imshow(lena[:,::-1])
plt.subplot(223)
plt.title('Sliced')
plt.axis('off') plt.imshow(lena[:lena.shape[0]/2,:lena.shape[1]/2])
mask = lena % 2 == 0
masked_lena = lena.copy()
masked_lena[mask] = 0
plt.subplot(224)
plt.title('Masked')
plt.axis('off')
plt.imshow(masked_lena)
plt.show()
花式索引
import scipy.misc
import matplotlib.pyplot as plt
lena = scipy.misc.lena()
height = lena.shape[0]
width = lena.shape[1]
lena[range(height), range(width)] = 0
lena[range(height), range(width - 1, -1, -1)] = 0
plt.imshow(lena)
plt.show()
将位置列表用于索引
import scipy.misc
import matplotlib.pyplot as plt
import numpy as np
lena = scipy.misc.lena()
height = lena.shape[0]
width = lena.shape[1]
def shuffle_indices(size):
'''
生成 0 ~ size - 1 的数组并打乱
'''
arr = np.arange(size)
np.random.shuffle(arr)
return arr
xindices = shuffle_indices(width)
np.testing.assert_equal(len(xindices), width)
yindices = shuffle_indices(height) np.testing.assert_equal(len(yindices), height)
plt.imshow(lena[np.ix_(yindices, xindices)])
plt.show()
布尔索引
import scipy.misc
import matplotlib.pyplot as plt
import numpy as np
lena = scipy.misc.lena()
def get_indices(size):
arr = np.arange(size)
return arr % 4 == 0
lena1 = lena.copy()
yindices = get_indices(lena.shape[0])
xindices = get_indices(lena.shape[1])
lena1[yindices, xindices] = 0
plt.subplot(211)
plt.imshow(lena1)
lena2 = lena.copy()
lena2[(lena > lena.max()/4) & (lena < 3 * lena.max()/4)] = 0
plt.subplot(212)
plt.imshow(lena2)
plt.show()
分离数独的九宫格
import numpy as np
sudoku = np.array([
[2, 8, 7, 1, 6, 5, 9, 4, 3],
[9, 5, 4, 7, 3, 2, 1, 6, 8],
[6, 1, 3, 8, 4, 9, 7, 5, 2],
[8, 7, 9, 6, 5, 1, 2, 3, 4],
[4, 2, 1, 3, 9, 8, 6, 7, 5],
[3, 6, 5, 4, 2, 7, 8, 9, 1],
[1, 9, 8, 5, 7, 3, 4, 2, 6],
[5, 4, 2, 9, 1, 6, 3, 8, 7],
[7, 3, 6, 2, 8, 4, 5, 1, 9]
])
shape = (3, 3, 3, 3)
strides = sudoku.itemsize * np.array([27, 3, 9, 1])
squares = np.lib.stride_tricks.as_strided(sudoku, shape=shape, strides=strides)
print(squares)
'''
[[[[2 8 7] [9 5 4] [6 1 3]]
[[1 6 5] [7 3 2] [8 4 9]]
[[9 4 3] [1 6 8] [7 5 2]]]
[[[8 7 9] [4 2 1] [3 6 5]]
[[6 5 1] [3 9 8] [4 2 7]]
[[2 3 4] [6 7 5] [8 9 1]]]
[[[1 9 8] [5 4 2] [7 3 6]]
[[5 7 3] [9 1 6] [2 8 4]]
[[4 2 6] [3 8 7] [5 1 9]]]]
'''
数组广播
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)
newdata = data * 0.2
newdata = newdata.astype(np.uint8)
print("Data type", newdata.dtype, "Shape", newdata.shape)
scipy.io.wavfile.write("quiet.wav", sample_rate, newdata)
plt.subplot(2, 1, 2)
plt.title("Quiet")
plt.plot(newdata)
plt.show()