首页 > 代码库 > 『Python』PIL图像处理_矩阵转化

『Python』PIL图像处理_矩阵转化

应师兄要求改图,因为使用了PIL包把图片对象转化为numpy的矩阵,截取以及处理很好玩且方便,特此记录:

 1 import numpy as np 2 from PIL import Image 3 import matplotlib.pyplot as plt 4  5 img = Image.open(./7b6021ef9e6892dcf14dc5dd269afaada763fedc13b29-iHXENu_fw658.jpeg) 6 plt.imshow(img) 7 plt.show() 8 # 转化为数组 9 img = np.asarray(img)10 print(图像矩阵尺寸:,img.shape)11 12 # 截取上面的图片,舍弃下面20行13 bottom = 5014 img = img[:-bottom,:]15 16 # 刻度数17 xscale = 518 yscale = 619 20 fig = plt.figure(Image)21 plt.imshow(img)22 # 按照师兄的要求生成坐标23 plt.xticks([img.shape[1]/xscale*w for w in range(xscale+1)], [%.1f % (87.3-(87.3-82.2)/xscale*w) for w in range(xscale+1)])24 plt.yticks([(img.shape[0]-bottom)/yscale*w for w in range(yscale+1)], [%.1f % (-3.5-(-3.5-(-(img.shape[1]-bottom)/img.shape[1]*(-3.5-1.5)-3.5))/yscale*w) for w in range(yscale+1)])25 plt.xlabel("Galactic Longitude")26 plt.ylabel("Galactic Latitude")27 plt.show()28 fig.savefig(result.eps,format=eps)29 fig.savefig(result.png,format=png)
/home/hellcat/anaconda2/envs/python3_6/bin/python /home/hellcat/PycharmProjects/data_analysis/帮师兄修图/handle_piecture.py图像矩阵尺寸: (924, 658, 3)Process finished with exit code 0

技术分享技术分享

 

顺便一提:

plt.xticks([img.shape[1]/xscale*w for w in range(xscale+1)], [%.1f‘ % (87.3-(87.3-82.2)/xscale*w) for w in range(xscale+1)])

这一句也超级好用的,虽然是matplotlib.pyplot包的,不过numpy.asarray()也不是PIL包的呀~~

『Python』PIL图像处理_矩阵转化