import numpy as np
import matplotlib.pyplot as plt
data = np.zeros((100,200,3),dtype="int") # zeros的shape參數(shù)為(100,200,3)表示是一個(gè)100行200列,每個(gè)點(diǎn)有3個(gè)元素的陣列侈沪。即長(zhǎng)200px,高100px的二維像素集潘拱,dtype="int"表示像素集可以用256的rgb的數(shù)組(list)表示能岩。zeros表示這個(gè)100行200列的像素陣列的每一個(gè)像素點(diǎn)都是rgb(0,0,0),即[0,0,0]
data
# data 數(shù)據(jù)
array([[[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
...,
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
...,
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
...,
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
...,
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
...,
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
...,
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
...,
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]])
img = plt.imshow(data) # 顯示圖像
data[::,::,0:1] = 255 # 將陣列的每一個(gè)點(diǎn)的第一個(gè)數(shù)值修改為255山涡,其他值不變
data # 再次查看data數(shù)據(jù)
# 每一行,每一列的像素點(diǎn)的第一個(gè)數(shù)值已經(jīng)變成了255阐肤,即rgb(255,0,0) 純紅色
array([[[255, 0, 0],
[255, 0, 0],
[255, 0, 0],
...,
[255, 0, 0],
[255, 0, 0],
[255, 0, 0]],
[[255, 0, 0],
[255, 0, 0],
[255, 0, 0],
...,
[255, 0, 0],
[255, 0, 0],
[255, 0, 0]],
[[255, 0, 0],
[255, 0, 0],
[255, 0, 0],
...,
[255, 0, 0],
[255, 0, 0],
[255, 0, 0]],
...,
[[255, 0, 0],
[255, 0, 0],
[255, 0, 0],
...,
[255, 0, 0],
[255, 0, 0],
[255, 0, 0]],
[[255, 0, 0],
[255, 0, 0],
[255, 0, 0],
...,
[255, 0, 0],
[255, 0, 0],
[255, 0, 0]],
[[255, 0, 0],
[255, 0, 0],
[255, 0, 0],
...,
[255, 0, 0],
[255, 0, 0],
[255, 0, 0]]])
plt.imshow(data) # 顯示圖像
<matplotlib.image.AxesImage at 0x222114e1b00>
data[::,::,0:1] = 0 # 先把每個(gè)像素點(diǎn)的第一個(gè)值歸0
data[::,::,1:2] = 255 # 再把每一個(gè)像素點(diǎn)的第2個(gè)值修改為255抬闯,即 rgb(0,255,0) 純綠色
data
array([[[ 0, 255, 0],
[ 0, 255, 0],
[ 0, 255, 0],
...,
[ 0, 255, 0],
[ 0, 255, 0],
[ 0, 255, 0]],
[[ 0, 255, 0],
[ 0, 255, 0],
[ 0, 255, 0],
...,
[ 0, 255, 0],
[ 0, 255, 0],
[ 0, 255, 0]],
[[ 0, 255, 0],
[ 0, 255, 0],
[ 0, 255, 0],
...,
[ 0, 255, 0],
[ 0, 255, 0],
[ 0, 255, 0]],
...,
[[ 0, 255, 0],
[ 0, 255, 0],
[ 0, 255, 0],
...,
[ 0, 255, 0],
[ 0, 255, 0],
[ 0, 255, 0]],
[[ 0, 255, 0],
[ 0, 255, 0],
[ 0, 255, 0],
...,
[ 0, 255, 0],
[ 0, 255, 0],
[ 0, 255, 0]],
[[ 0, 255, 0],
[ 0, 255, 0],
[ 0, 255, 0],
...,
[ 0, 255, 0],
[ 0, 255, 0],
[ 0, 255, 0]]])
plt.imshow(data)
<matplotlib.image.AxesImage at 0x222115452e8>
data[::,::,1:2] = 0 # 把每一個(gè)像素點(diǎn)的第2個(gè)值歸0
data[::,::,2:3] = 255 # 第3個(gè)值改為255,rgb(0,0,255) 純藍(lán)色
data
array([[[ 0, 0, 255],
[ 0, 0, 255],
[ 0, 0, 255],
...,
[ 0, 0, 255],
[ 0, 0, 255],
[ 0, 0, 255]],
[[ 0, 0, 255],
[ 0, 0, 255],
[ 0, 0, 255],
...,
[ 0, 0, 255],
[ 0, 0, 255],
[ 0, 0, 255]],
[[ 0, 0, 255],
[ 0, 0, 255],
[ 0, 0, 255],
...,
[ 0, 0, 255],
[ 0, 0, 255],
[ 0, 0, 255]],
...,
[[ 0, 0, 255],
[ 0, 0, 255],
[ 0, 0, 255],
...,
[ 0, 0, 255],
[ 0, 0, 255],
[ 0, 0, 255]],
[[ 0, 0, 255],
[ 0, 0, 255],
[ 0, 0, 255],
...,
[ 0, 0, 255],
[ 0, 0, 255],
[ 0, 0, 255]],
[[ 0, 0, 255],
[ 0, 0, 255],
[ 0, 0, 255],
...,
[ 0, 0, 255],
[ 0, 0, 255],
[ 0, 0, 255]]])
plt.imshow(data)
<matplotlib.image.AxesImage at 0x2221159c908>
data[::,::,2:3] = 0 # 把每一個(gè)像素點(diǎn)的第3個(gè)值歸0
data[::,::,0:2] = 255 # 前2個(gè)值改為255特咆,rgb(255,255,0)
data
array([[[255, 255, 0],
[255, 255, 0],
[255, 255, 0],
...,
[255, 255, 0],
[255, 255, 0],
[255, 255, 0]],
[[255, 255, 0],
[255, 255, 0],
[255, 255, 0],
...,
[255, 255, 0],
[255, 255, 0],
[255, 255, 0]],
[[255, 255, 0],
[255, 255, 0],
[255, 255, 0],
...,
[255, 255, 0],
[255, 255, 0],
[255, 255, 0]],
...,
[[255, 255, 0],
[255, 255, 0],
[255, 255, 0],
...,
[255, 255, 0],
[255, 255, 0],
[255, 255, 0]],
[[255, 255, 0],
[255, 255, 0],
[255, 255, 0],
...,
[255, 255, 0],
[255, 255, 0],
[255, 255, 0]],
[[255, 255, 0],
[255, 255, 0],
[255, 255, 0],
...,
[255, 255, 0],
[255, 255, 0],
[255, 255, 0]]])
plt.imshow(data)
<matplotlib.image.AxesImage at 0x222115f3eb8>
data[::,::,0:2] = 0 # 每個(gè)像素點(diǎn)的前2個(gè)值歸0季惩,rgb(0,0,0)
data[::,::,1:3] = 255 # 后2個(gè)值改為255,rgb(0,255,255)
data
array([[[ 0, 255, 255],
[ 0, 255, 255],
[ 0, 255, 255],
...,
[ 0, 255, 255],
[ 0, 255, 255],
[ 0, 255, 255]],
[[ 0, 255, 255],
[ 0, 255, 255],
[ 0, 255, 255],
...,
[ 0, 255, 255],
[ 0, 255, 255],
[ 0, 255, 255]],
[[ 0, 255, 255],
[ 0, 255, 255],
[ 0, 255, 255],
...,
[ 0, 255, 255],
[ 0, 255, 255],
[ 0, 255, 255]],
...,
[[ 0, 255, 255],
[ 0, 255, 255],
[ 0, 255, 255],
...,
[ 0, 255, 255],
[ 0, 255, 255],
[ 0, 255, 255]],
[[ 0, 255, 255],
[ 0, 255, 255],
[ 0, 255, 255],
...,
[ 0, 255, 255],
[ 0, 255, 255],
[ 0, 255, 255]],
[[ 0, 255, 255],
[ 0, 255, 255],
[ 0, 255, 255],
...,
[ 0, 255, 255],
[ 0, 255, 255],
[ 0, 255, 255]]])
plt.imshow(data)
<matplotlib.image.AxesImage at 0x22211654470>
data[::,::,1:2] = 0 # 每個(gè)像素點(diǎn)的第個(gè)值歸0腻格,rgb(0,0,255)
data[::,::,0:1] = 255 # 第1個(gè)值改為255画拾,rgb(255,0,255)
data
array([[[255, 0, 255],
[255, 0, 255],
[255, 0, 255],
...,
[255, 0, 255],
[255, 0, 255],
[255, 0, 255]],
[[255, 0, 255],
[255, 0, 255],
[255, 0, 255],
...,
[255, 0, 255],
[255, 0, 255],
[255, 0, 255]],
[[255, 0, 255],
[255, 0, 255],
[255, 0, 255],
...,
[255, 0, 255],
[255, 0, 255],
[255, 0, 255]],
...,
[[255, 0, 255],
[255, 0, 255],
[255, 0, 255],
...,
[255, 0, 255],
[255, 0, 255],
[255, 0, 255]],
[[255, 0, 255],
[255, 0, 255],
[255, 0, 255],
...,
[255, 0, 255],
[255, 0, 255],
[255, 0, 255]],
[[255, 0, 255],
[255, 0, 255],
[255, 0, 255],
...,
[255, 0, 255],
[255, 0, 255],
[255, 0, 255]]])
plt.imshow(data)
<matplotlib.image.AxesImage at 0x222114af908>