『Python』PIL影像處理_矩陣轉化

來源:互聯網
上載者:User

標籤: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影像處理_矩陣轉化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.