# 图像数据增强实战

[python]
# 加载图像
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

img = Image.open(‘./NIKE.png’)
img = np.array(img)
plt.imshow(img)
plt.show()
[/python]

[python]
# 用 Numpy 翻转
flipped_img = np.fliplr(img)
plt.imshow(flipped_img)
plt.show()
[/python]

[python]
# 向左平移
for i in range(HEIGHT, 1, -1):
for j in range(WIDTH):
if (i < HEIGHT-20):
img[j][i] = img[j][i-20]
elif (i < HEIGHT-1):
img[j][i] = 0

plt.imshow(img)
plt.show()
[/python]

[python]
# 向右平移
for j in range(WIDTH):
for i in range(HEIGHT):
if (i < HEIGHT-20):
img[j][i] = img[j][i+20]

plt.imshow(img)
plt.show()
[/python]

[python]
# 向上平移
for j in range(WIDTH):
for i in range(HEIGHT):
if (j < WIDTH – 20 and j > 20):
img[j][i] = img[j+20][i]
else:
img[j][i] = 0

plt.imshow(img)
plt.show()/python]

<img class="alignnone size-full wp-image-3554 aligncenter" src="http://www.tensorflownews.com/wp-content/uploads/2018/10/1_Bx6oTpNKdG2o-31nFztB-w.png" alt="" width="800" height="374" />

[python]
# 向下平移
for j in range(WIDTH, 1, -1):
for i in range(278):
if (j < 144 and j > 20):
img[j][i] = img[j-20][i]

plt.imshow(img)
plt.show()
[/python]

[python]
# 添加噪声
noise = np.random.randint(5, size = (164,278, 4), dtype = ‘uint8’)

for i in range(WIDTH):
for j in range(HEIGHT):
for k in range(DEPTH):
if (img[i][j][k] != 255):
img[i][j][k] += noise[i][j][k]

plt.imshow(img)
plt.show()
[/python]