numpy图片操作练习
将鱼图像数据进行操作,使用numpy知识
导入图片
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
fish = plt.imread('fish.png')
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.28235295, 0.37254903, 0.4392157 ],
[0.6666667 , 0.6627451 , 0.654902 ],
[1. , 1. , 1. ],
...,
[1. , 1. , 1. ],
[1. , 1. , 1. ],
[0.35686275, 0.4745098 , 0.5764706 ]],
...,
[[0.4509804 , 0.45882353, 0.45882353],
[0.6509804 , 0.6509804 , 0.64705884],
[0.99215686, 0.99215686, 0.9843137 ],
...,
[1. , 0.99607843, 0.9882353 ],
[0.9843137 , 0.9882353 , 0.98039216],
[0.36078432, 0.49019608, 0.6 ]],
[[0.4509804 , 0.45882353, 0.45882353],
[0.6509804 , 0.6509804 , 0.64705884],
[0.99215686, 0.99215686, 0.9843137 ],
...,
[1. , 0.99607843, 0.9882353 ],
[0.9843137 , 0.9882353 , 0.98039216],
[0.36078432, 0.49019608, 0.6 ]],
[[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)
plt.imshow(fish)
<matplotlib.image.AxesImage at 0x7fc9381b8fd0>
一个三维随即数组
n = np.random.randint(0, 20, size=(3, 4, 5))
n
array([[[ 7, 3, 19, 3, 12],
[12, 13, 19, 4, 14],
[12, 14, 7, 3, 11],
[ 5, 9, 16, 0, 6]],
[[ 7, 1, 19, 13, 1],
[18, 14, 3, 7, 3],
[18, 1, 4, 16, 10],
[ 9, 17, 1, 16, 11]],
[[ 8, 7, 7, 1, 1],
[12, 6, 15, 16, 11],
[ 3, 2, 10, 3, 7],
[15, 19, 17, 18, 5]]])
上下颠倒
fish1 = fish[::-1]
plt.imshow(fish1)
<matplotlib.image.AxesImage at 0x7fc9368d9470>
n1 = n[::-1]
n1
array([[[ 8, 7, 7, 1, 1],
[12, 6, 15, 16, 11],
[ 3, 2, 10, 3, 7],
[15, 19, 17, 18, 5]],
[[ 7, 1, 19, 13, 1],
[18, 14, 3, 7, 3],
[18, 1, 4, 16, 10],
[ 9, 17, 1, 16, 11]],
[[ 7, 3, 19, 3, 12],
[12, 13, 19, 4, 14],
[12, 14, 7, 3, 11],
[ 5, 9, 16, 0, 6]]])
左右颠倒
fish2 = fish[:, ::-1]
plt.imshow(fish2)
<matplotlib.image.AxesImage at 0x7fc9368b9ac8>
n2 = n[:, ::-1]
n2
array([[[ 5, 9, 16, 0, 6],
[12, 14, 7, 3, 11],
[12, 13, 19, 4, 14],
[ 7, 3, 19, 3, 12]],
[[ 9, 17, 1, 16, 11],
[18, 1, 4, 16, 10],
[18, 14, 3, 7, 3],
[ 7, 1, 19, 13, 1]],
[[15, 19, 17, 18, 5],
[ 3, 2, 10, 3, 7],
[12, 6, 15, 16, 11],
[ 8, 7, 7, 1, 1]]])
颜色颠倒
fish3 = fish[:, :, ::-1]
plt.imshow(fish3)
<matplotlib.image.AxesImage at 0x7fc936818e48>
n3 = n[:, :, ::-1]
n3
array([[[12, 3, 19, 3, 7],
[14, 4, 19, 13, 12],
[11, 3, 7, 14, 12],
[ 6, 0, 16, 9, 5]],
[[ 1, 13, 19, 1, 7],
[ 3, 7, 3, 14, 18],
[10, 16, 4, 1, 18],
[11, 16, 1, 17, 9]],
[[ 1, 1, 7, 7, 8],
[11, 16, 15, 6, 12],
[ 7, 3, 10, 2, 3],
[ 5, 18, 17, 19, 15]]])
降低精度
fish4 = fish[::4, ::4]
plt.imshow(fish4)
<matplotlib.image.AxesImage at 0x7fc9367f4f60>
n4 = n[::2, ::2]
n4
array([[[ 7, 3, 19, 3, 12],
[12, 14, 7, 3, 11]],
[[ 8, 7, 7, 1, 1],
[ 3, 2, 10, 3, 7]]])
打马赛克
fish5 = fish[:, ::-1]
plt.imshow(fish5)
<matplotlib.image.AxesImage at 0x7fc936745710>
fish5[50:100, 60:120] = 0
plt.imshow(fish5)
<matplotlib.image.AxesImage at 0x7fc9366a1908>
n5 = n
n5[:2, 2:3] = 0
n5
array([[[ 7, 3, 19, 3, 12],
[12, 13, 19, 4, 14],
[ 0, 0, 0, 0, 0],
[ 5, 9, 16, 0, 6]],
[[ 7, 1, 19, 13, 1],
[18, 14, 3, 7, 3],
[ 0, 0, 0, 0, 0],
[ 9, 17, 1, 16, 11]],
[[ 8, 7, 7, 1, 1],
[12, 6, 15, 16, 11],
[ 3, 2, 10, 3, 7],
[15, 19, 17, 18, 5]]])