python實現圖像處理之PiL依賴庫的案例應用詳解
本文包含的練習題主要是PIL依賴庫,即pillow相關的應用。
練習一:使用python給圖片增加數字
實現思路:
使用PIL的Image.open導入圖片。 獲取圖片的大小。 調用ImageDraw,在圖片的指定位置寫上數字。#coding=utf-8#Auther by Alice#在圖片的右上角增加一個數字 from PIL import Image,ImageFont,ImageDrawimage = Image.open(’/Users/alice/Documents/Photo/IMG_8379.JPG’)#打開原圖 wight, hight = image.sizetext = '015'color = (255,255,0)fontsize = wight//10font = ImageFont.truetype(’Apple Symbols’,fontsize)#設定增加的數字的參數,數字內容、數字顏色和數字字號 draw = ImageDraw.Draw(image)draw.text((fontsize*6,0), text, color, font)image.save(’/Users/alice/Documents/Photo/IMG_7997.JPG’, ’jpeg’)#保存添加了數字之后的圖片
實現前:
實現后:
修改其中兩行代碼字體和顏色如下后,
color = (105,200,45)font = ImageFont.truetype(’Palatino.ttc’,fontsize)
則運行的結果為:
練習二:使用python將一個圖片放大縮小
實現思路:
使用PIL,即Python圖像標準依賴庫。 使用open打開本地圖片。 使用image.thumbnail放大縮小圖片#coding by alice#coding=utf-8 from PIL import Image im = Image.open(’/Users/alice/Documents/Develop/PythonCode/test.JPG’)# 打開一個路徑下的指定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’)# 打開一個路徑下的指定jpg圖像文件wight,hight = image.size# 獲得圖像尺寸image.thumbnail((weight//10, high//10))# 縮放到10%image.save(’/Users/alice/Documents/Develop/PythonCode/test2.JPG’, ’jpeg’)# 把縮放后的圖像用jpg格式保存:
運行后的效果為:
練習三:使用python將一個圖片實現模糊
實現思路:
使用PIL,即Python圖像標準依賴庫。 使用open打開本地圖片。 使用image.thumbnail放大縮小圖片#coding by alice#coding=utf-8 from PIL import Imagefrom PIL import ImageFilter image = Image.open(’/Users/alice/Documents/Develop/PythonCode/test.JPG’)# 打開一個路徑下的jpg圖像文件image = image.filter(ImageFilter.BLUR)# 應用模糊濾鏡image.save(’/Users/alice/Documents/Develop/PythonCode/test3.JPG’, ’jpeg’)#保存圖片
運行后的結果為
如果是靜物或者人臉,放大后看則模糊效果會更明顯。
練習四:使用python獲取一個圖片的元素坐標
實現思路:
使用PIL,即Python圖像標準依賴庫。 使用open打開本地圖片。 使用imshow顯示圖像 獲取圖片上點擊光標,輸出坐標#coding by alice#coding=utf-8 from PIL import Imageimport matplotlib.pyplot as plt image = Image.open(’/Users/alice/Documents/Develop/PythonCode/test.JPG’)#打開所在位置及圖像的名稱plt.figure(’image’)#圖像窗口名稱plt.imshow(image)plt.show()
到此這篇關于python實現圖像處理之PiL依賴庫的案例應用詳解的文章就介紹到這了,更多相關python實現圖像處理之PiL依賴庫內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: