標籤:tom numpy xen from scale rom 轉化 截取 .com
應師兄要求改圖,因為使用了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影像處理_矩陣轉化