手工画图转化适合MNIST的灰度图像
因为电脑没有安装photoshop,所以用windows自带画图软件进行测试,相关配置如下图:
但,通过此方法画出的图像是RGB三通道,需要先转化成与MNIST相同的灰度图像。相关方法可查看下面两篇博客:
图像处理库PIL中图像格式转换(一)
转化成灰度图像
from PIL import Image,ImageOps
import matplotlib.pyplot as plt
#自己画图的存放地址
picture=Image.open(r'D:/LearnPython/JupyterNotebook/Python3/First_deep_Learning/number3.png')
# print(picture)
# print(picture.mode)
# print(picture.getpixel((15,10)))
picture_L=picture.convert('L',)
# print(picture_L)
# print(picture_L.size)
# print(picture_L.getpixel((15,10)))
# print(picture_L.mode)
inverted_image=ImageOps.invert(picture_L)#因为画图软件是白底黑字,与MNIST相反,所以要反转一下
# plt.figure(figsize=(15,15))
# plt.tick_params(colors='white')#设置图片坐标刻度颜色
######生成的图像已经归一化,像素点数值在0-1之间
inverted_image.save(r'D:\LearnPython\number3_gray.png')#保存转化后的图像
plt.figure(figsize=(15,10))
plt.tick_params(colors='white') #设置坐标刻度颜色
plt.imshow(inverted_image) #显示要预测的瑞利图(默认显示)
运行结果:
plt.imshow(inverted_image, cmap="gray") # 显示要预测的图片(灰度图)
运行结果: