Python編程圖形庫(kù)之Pillow使用方法講解
PIL vs Pillow
PIL: Python Imaging Library,是python的圖像處理庫(kù)。由于PIL不兼容setuptools,再加上更新緩慢等因素,Alex Clark等一些社區(qū)好心人還是希望能夠繼續(xù)支持PIL,所以fork了PIL,這就是Pillow的緣起。
Pillow的目標(biāo)
推動(dòng)和促進(jìn)PIL的發(fā)展是Pillow的目標(biāo),主要通過(guò)如下的方式來(lái)進(jìn)行
- 結(jié)合Travis CI和AppVeyor進(jìn)行持續(xù)集成測(cè)試
- 活用github進(jìn)行開(kāi)發(fā)
- 結(jié)合Python Package Index進(jìn)行例行發(fā)布
其實(shí)可以看出,所做的改善就是在CI和CD,改善用戶(hù)感知,定期/快速地與使用者進(jìn)行溝通和交流,是pillow獲得好感的一個(gè)重要因素。
安裝
安裝可以通過(guò)pip,只需要執(zhí)行pip install pillow即可
liumiaocn:~ liumiao$ pip install pillow Collecting pillow Downloading https://files.pythonhosted.org/packages/df/aa/a25f211a4686f363d8ca5a1752c43a8f42459e70af13e20713d3e636f0af/Pillow-5.1.0-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (3.6MB) 100% |████████████████████████████████| 3.6MB 157kB/s Installing collected packages: pillow Successfully installed pillow-5.1.0 liumiaocn:~ liumiao$
安裝確認(rèn)
liumiaocn:~ liumiao$ pip show pillow Name: Pillow Version: 5.1.0 Summary: Python Imaging Library (Fork) Home-page: https://python-pillow.org Author: Alex Clark (Fork Author) Author-email: aclark@aclark.net License: Standard PIL License Location: /usr/local/lib/python2.7/site-packages Requires: Required-by: liumiaocn:~ liumiao$
使用
圖形庫(kù)有很多實(shí)用的功能,這里列舉幾個(gè)進(jìn)行簡(jiǎn)單演示。
ImageGrab.grab()
使用這個(gè)方法可以實(shí)現(xiàn)抓屏:
liumiaocn:tmp liumiao$ cat grab.python #!/usr/local/bin/python from PIL import ImageGrab #get current screen copy image = ImageGrab.grab() #display image size print("Current screen shot size :",image.size) #display image mode print("Screen shot picture mode :", image.mode) #save picture to /tmp/screen-grab-1.bmp image.save('/tmp/screen-grab-1.bmp') #show picture image.show() liumiaocn:tmp liumiao$
因?yàn)榇a中使用了image.show()進(jìn)行了顯示,執(zhí)行之后可以直接看到顯示,同時(shí)也能確認(rèn)到/tmp下所生成的文件
liumiaocn:tmp liumiao$ python grab.python ('Current screen shot size :', (2880, 1800)) ('Screen shot picture mode :', 'RGBA') liumiaocn:tmp liumiao$ ls -l /tmp/screen-grab-1.bmp -rw-r--r-- 1 liumiao wheel 20736054 Jun 23 05:41 /tmp/screen-grab-1.bmp liumiaocn:tmp liumiao$
濾鏡
PIL中的ImageFilter支持近十種濾鏡, 比如對(duì)剛剛抓取的圖片使用CONTOUR濾鏡
liumiaocn:tmp liumiao$ cat filter-contour.py #!/usr/local/bin/python from PIL import ImageFilter, Image src_image = Image.open('/tmp/screen-grab-1.bmp') print("begin to filter the pic") dst_image = src_image.filter(ImageFilter.CONTOUR) print("picture through filter") dst_image.show() liumiaocn:tmp liumiao$
執(zhí)行之后可以得到如下圖片
旋轉(zhuǎn)
使用rotate即可對(duì)圖片進(jìn)行旋轉(zhuǎn)操作:
liumiaocn:tmp liumiao$ cat rotate.py #!/usr/local/bin/python from PIL import Image src_image = Image.open('/tmp/screen-grab-1.bmp') print("begin to rotate the pic") dst_image = src_image.rotate(90) print("picture after rotating") dst_image.show() liumiaocn:tmp liumiao$
執(zhí)行之后,即可確認(rèn)
Pillow功能非常之多,而且使用也很方便,比如resize對(duì)尺寸進(jìn)行調(diào)節(jié),還可以添加文字等等常見(jiàn)的圖形處理操作,這里就不再一一介紹,具體的需要可以參看如下鏈接進(jìn)行了解:https://pypi.org/project/Pillow/
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
- Python基于pillow庫(kù)實(shí)現(xiàn)生成圖片水印
- python實(shí)現(xiàn)按鍵精靈找色點(diǎn)擊功能教程,使用pywin32和Pillow庫(kù)
- Python3 使用pillow庫(kù)生成隨機(jī)驗(yàn)證碼
- Linux上安裝Python的PIL和Pillow庫(kù)處理圖片的實(shí)例教程
- 在Mac OS系統(tǒng)上安裝Python的Pillow庫(kù)的教程
- Python利用Pillow(PIL)庫(kù)實(shí)現(xiàn)驗(yàn)證碼圖片的全過(guò)程
- Python Pillow(PIL)庫(kù)的用法詳解
- 詳解Python圖像處理庫(kù)Pillow常用使用方法
- python pillow庫(kù)的基礎(chǔ)使用教程
相關(guān)文章
Python3實(shí)現(xiàn)發(fā)送郵件和發(fā)送短信驗(yàn)證碼功能
這篇文章主要介紹了Python3實(shí)現(xiàn)發(fā)送郵件和發(fā)送短信驗(yàn)證碼功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01python自動(dòng)化測(cè)試selenium定位frame及iframe示例
這篇文章主要為大家介紹了python自動(dòng)化測(cè)試selenium定位frame及iframe示例的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11詳解python3 + Scrapy爬蟲(chóng)學(xué)習(xí)之創(chuàng)建項(xiàng)目
這篇文章主要介紹了python3 Scrapy爬蟲(chóng)創(chuàng)建項(xiàng)目,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04Pycharm學(xué)習(xí)教程(4) Python解釋器的相關(guān)配置
這篇文章主要為大家詳細(xì)介紹了最全的Pycharm學(xué)習(xí)教程第四篇,Python解釋器配置,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05對(duì)于Python中線(xiàn)程問(wèn)題的簡(jiǎn)單講解
這篇文章主要介紹了對(duì)于Python中線(xiàn)程問(wèn)題的簡(jiǎn)單講解,線(xiàn)程一直是Python編程當(dāng)中的熱點(diǎn)問(wèn)題,而本文沒(méi)有涉及GIL線(xiàn)程鎖方面的內(nèi)容,需要的朋友可以參考下2015-04-04