将图像二值化,像素值大于128的变为1,否则变为0
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
img = np.array(Image.open('pokemon.jpg').convert('L')) #打开图片
rows,cols=img.shape #获取像素坐标(rows,cols)
for i in range(rows):
for j in range(cols):
if (img[i,j]<=128): #判断该点的值
img[i,j]=0
else:
img[i,j]=1
plt.figure("lena") #lena嘛
plt.imshow(img,cmap='gray')
plt.axis('off')
plt.show()