python實(shí)現(xiàn)圖像處理之PiL依賴庫(kù)的案例應(yīng)用詳解
Python實(shí)現(xiàn)圖像處理:PiL依賴庫(kù)的應(yīng)用
本文包含的練習(xí)題主要是PIL依賴庫(kù),即pillow相關(guān)的應(yīng)用。
練習(xí)一:使用python給圖片增加數(shù)字
實(shí)現(xiàn)思路:
- 使用PIL的Image.open導(dǎo)入圖片。
- 獲取圖片的大小。
- 調(diào)用ImageDraw,在圖片的指定位置寫上數(shù)字。
#coding=utf-8
#Auther by Alice
#在圖片的右上角增加一個(gè)數(shù)字
from PIL import Image,ImageFont,ImageDraw
image = Image.open('/Users/alice/Documents/Photo/IMG_8379.JPG')
#打開原圖
wight, hight = image.size
text = "015"
color = (255,255,0)
fontsize = wight//10
font = ImageFont.truetype('Apple Symbols',fontsize)
#設(shè)定增加的數(shù)字的參數(shù),數(shù)字內(nèi)容、數(shù)字顏色和數(shù)字字號(hào)
draw = ImageDraw.Draw(image)
draw.text((fontsize*6,0), text, color, font)
image.save('/Users/alice/Documents/Photo/IMG_7997.JPG', 'jpeg')
#保存添加了數(shù)字之后的圖片
實(shí)現(xiàn)前:

實(shí)現(xiàn)后:

修改其中兩行代碼字體和顏色如下后,
color = (105,200,45)
font = ImageFont.truetype('Palatino.ttc',fontsize)
則運(yùn)行的結(jié)果為:

練習(xí)二:使用python將一個(gè)圖片放大縮小
實(shí)現(xiàn)思路:
- 使用PIL,即Python圖像標(biāo)準(zhǔn)依賴庫(kù)。
- 使用open打開本地圖片。
- 使用image.thumbnail放大縮小圖片
#coding by alice
#coding=utf-8
from PIL import Image
im = Image.open('/Users/alice/Documents/Develop/PythonCode/test.JPG')
# 打開一個(gè)路徑下的指定jpg圖像文件
w,h = im.size
# 獲得圖像尺寸
im.thumbnail((w//10, h//10))
# 縮放到10%
im.save('/Users/alice/Documents/Develop/PythonCode/test2.JPG', 'jpeg')
# 把縮放后的圖像用jpeg格式保存:
等同于代碼:
#coding by alice
#coding=utf-8
from PIL import Image
image = Image.open('/Users/alice/Documents/Develop/PythonCode/test.JPG')
# 打開一個(gè)路徑下的指定jpg圖像文件
wight,hight = image.size
# 獲得圖像尺寸
image.thumbnail((weight//10, high//10))
# 縮放到10%
image.save('/Users/alice/Documents/Develop/PythonCode/test2.JPG', 'jpeg')
# 把縮放后的圖像用jpg格式保存:
運(yùn)行后的效果為:

練習(xí)三:使用python將一個(gè)圖片實(shí)現(xiàn)模糊
實(shí)現(xiàn)思路:
- 使用PIL,即Python圖像標(biāo)準(zhǔn)依賴庫(kù)。
- 使用open打開本地圖片。
- 使用image.thumbnail放大縮小圖片
#coding by alice
#coding=utf-8
from PIL import Image
from PIL import ImageFilter
image = Image.open('/Users/alice/Documents/Develop/PythonCode/test.JPG')
# 打開一個(gè)路徑下的jpg圖像文件
image = image.filter(ImageFilter.BLUR)
# 應(yīng)用模糊濾鏡
image.save('/Users/alice/Documents/Develop/PythonCode/test3.JPG', 'jpeg')
#保存圖片
運(yùn)行后的結(jié)果為

如果是靜物或者人臉,放大后看則模糊效果會(huì)更明顯。

練習(xí)四:使用python獲取一個(gè)圖片的元素坐標(biāo)
實(shí)現(xiàn)思路:
- 使用PIL,即Python圖像標(biāo)準(zhǔn)依賴庫(kù)。
- 使用open打開本地圖片。
- 使用imshow顯示圖像
- 獲取圖片上點(diǎn)擊光標(biāo),輸出坐標(biāo)
#coding by alice
#coding=utf-8
from PIL import Image
import matplotlib.pyplot as plt
image = Image.open('/Users/alice/Documents/Develop/PythonCode/test.JPG')
#打開所在位置及圖像的名稱
plt.figure('image')
#圖像窗口名稱
plt.imshow(image)
plt.show()

到此這篇關(guān)于python實(shí)現(xiàn)圖像處理之PiL依賴庫(kù)的案例應(yīng)用詳解的文章就介紹到這了,更多相關(guān)python實(shí)現(xiàn)圖像處理之PiL依賴庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
完美解決torch.cuda.is_available()一直返回False的玄學(xué)方法
這篇文章主要介紹了完美解決torch.cuda.is_available()一直返回False的玄學(xué)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
Python黑魔法Descriptor描述符的實(shí)例解析
與迭代器和裝飾器等一樣,描述符也是Python編程中的一項(xiàng)高級(jí)技巧,這里我們就來(lái)講解Python黑魔法Descriptor描述符的實(shí)例解析:2016-06-06
Python中還原JavaScript的escape函數(shù)編碼后字符串的方法
這篇文章主要介紹了Python中解析JavaScript的escape函數(shù)編碼后字符串的方法,即Python中如何還原JavaScript escape函數(shù)編碼后的字符串,需要的朋友可以參考下2014-08-08

