说明: 在python中,图片可以看成一个三维的矩阵,第一维控制着垂直方向,第二维控制着水平方向,第三维控制颜色。
import numpy as np
import matplotlib as plt
%matplotlib inline
使用%matplotlib inline可以省略plt.show()
fish = plt.imread('./fish.png')
plt.imshow(fish)
得到的fish
对象的值则为图片的矩阵
array([[[0.29411766, 0.39215687, 0.46666667],
[0.46666667, 0.4862745 , 0.49803922],
[0.4627451 , 0.4862745 , 0.5019608 ],
...,
[0.4627451 , 0.48235294, 0.49803922],
[0.45882353, 0.47843137, 0.49803922],
[0.21960784, 0.33333334, 0.44313726]],
[[0.2901961 , 0.3764706 , 0.44313726],
[0.627451 , 0.6156863 , 0.60784316],
[0.85490197, 0.85490197, 0.84705883],
...,
[0.8627451 , 0.85882354, 0.8509804 ],
[0.8509804 , 0.8509804 , 0.84313726],
[0.30588236, 0.42352942, 0.5254902 ]],
[[0.44705883, 0.45490196, 0.45490196],
[0.65882355, 0.654902 , 0.654902 ],
[1. , 1. , 1. ],
...,
[1. , 1. , 1. ],
[1. , 1. , 1. ],
[0.36078432, 0.49411765, 0.6 ]]], dtype=float32)
效果:
fish2 = fish[::-1]
plt.imshow(fish2)
fish3 = fish[::, ::-1]
plt.imshow(fish3)
fish4 = fish[::, ::, ::-1]
plt.imshow(fish4)
fish5 = fish[::4, ::4]
plt.imshow(fish5)
fish6 = fish.copy()
fish6[70:90, 80:120] = 0
plt.imshow(fish6)