python PIL Image 圖像處理基本操作實(shí)例
1. 圖片加載、灰度圖、 顯示和保存
from PIL import Image
img = Image.open('01.jpg')
imgGrey = img.convert('L')
img.show()
imgGrey.show()
img.save('img_copy.jpg')
imgGrey.save('img_gray.jpg')2. 圖片寬、高、通道模式、平均值獲取
from PIL import Image
import numpy as np
img = Image.open('01.jpg')
width, height = img.size
channel_mode = img.mode
mean_value = np.mean(img)
print(width)
print(height)
print(channel_mode)
print(mean_value)3. 創(chuàng)建指定大小,指定通道類型的空?qǐng)D像
from PIL import Image
width = 200
height = 100
img_white = Image.new('RGB', (width,height), (255,255,255))
img_black = Image.new('RGB', (width,height), (0,0,0))
img_L = Image.new('L', (width, height), (255))
img_white.show()
img_black.show()
img_L.show()4. 訪問(wèn)和操作圖像像素
from PIL import Image
img = Image.open('01.jpg')
width, height = img.size
# 獲取指定坐標(biāo)位置像素值
pixel_value = img.getpixel((width/2, height/2))
print(pixel_value)
# 或者使用load方法
pim = img.load()
pixel_value1 = pim[width/2, height/2]
print(pixel_value1)
# 設(shè)置指定坐標(biāo)位置像素的值
pim[width/2, height/2] = (0, 0, 0)
# 或使用putpixel方法
img.putpixel((w//2, h//2), (255,255,255))
# 設(shè)置指定區(qū)域像素的值
for w in range(int(width/2) - 40, int(width/2) + 40):
for h in range(int(height/2) - 20, int(height/2) + 20):
pim[w, h] = (255, 0, 0)
# img.putpixel((w, h), (255,255,255))
img.show()5. 圖像通道分離和合并
from PIL import Image
img = Image.open('01.jpg')
# 通道分離
R, G, B = img.split()
R.show)
G.show()
B.show()
# 通道合并
img_RGB = Image.merge('RGB', (R, G, B))
img_BGR = Image.merge('RGB', (B, G, R))
img_RGB.show()
img_BGR.show()6. 在圖像上輸出文字
from PIL import Image, ImageDraw, ImageFont
img = Image.open('01.jpg')
# 創(chuàng)建Draw對(duì)象:
draw = ImageDraw.Draw(img)
# 字體顏色
fillColor = (255, 0, 0)
text = 'print text on PIL Image'
position = (200,100)
draw.text(position, text, fill=fillColor)
img.show()7. 圖像縮放
from PIL import Image
img = Image.open('01.jpg')
width, height = img.size
img_NEARESET = img.resize((width//2, height//2)) # 縮放默認(rèn)模式是NEARESET(最近鄰插值)
img_BILINEAR = img.resize((width//2, height//2), Image.BILINEAR) # BILINEAR 2x2區(qū)域的雙線性插值
img_BICUBIC = img.resize((width//2, height//2), Image.BICUBIC) # BICUBIC 4x4區(qū)域的雙三次插值
img_ANTIALIAS = img.resize((width//2, height//2), Image.ANTIALIAS) # ANTIALIAS 高質(zhì)量下采樣濾波8. 圖像遍歷操作
from PIL import Image
img = Image.open('01.jpg').convert('L')
width, height = img.size
pim = img.load()
for w in range(width):
for h in range(height):
if pim[w, h] > 100:
img.putpixel((w, h), 255)
# pim[w, h] = 255
else:
img.putpixel((w, h), 0)
# pim[w, h] = 0
img.show()9. 圖像閾值分割、 二值化
from PIL import Image
img = Image.open('01.jpg').convert('L')
width, height = img.size
threshold = 125
for w in range(width):
for h in range(height):
if img.getpixel((w, h)) > threshold:
img.putpixel((w, h), 255)
else:
img.putpixel((w, h), 0)
img.save('binary.jpg')10. 圖像裁剪
from PIL import Image
img = Image.open('01.jpg')
width, height = img.size
# 前兩個(gè)坐標(biāo)點(diǎn)是左上角坐標(biāo)
# 后兩個(gè)坐標(biāo)點(diǎn)是右下角坐標(biāo)
# width在前, height在后
box = (100, 100, 550, 350)
region = img.crop(box)
region.save('crop.jpg')11. 圖像邊界擴(kuò)展
# 邊界擴(kuò)展
from PIL import Image
img = Image.open('test.png')
width, height = img.size
channel_mode = img.mode
img_makeBorder_full = Image.new(channel_mode, (2*width, height))
img_makeBorder_part = Image.new(channel_mode, (width+200, height))
# 圖像水平擴(kuò)展整個(gè)圖像
img_makeBorder_full.paste(img, (0, 0, width, height))
img_makeBorder_full.paste(img, (width, 0, 2*width, height))
# 前兩個(gè)坐標(biāo)點(diǎn)是左上角坐標(biāo)
# 后兩個(gè)坐標(biāo)點(diǎn)是右下角坐標(biāo)
# width在前, height在后
box = (width-200, 0, width, height)
region = img.crop(box)
# 圖像水平右側(cè)擴(kuò)展一個(gè)ROI
img_makeBorder_part.paste(img, (0, 0, width, height))
img_makeBorder_part.paste(region, (width, 0, width+200, height))
img_makeBorder_part.show()
img_makeBorder_full.show()12. PIL.Image 和 numpy 格式相互轉(zhuǎn)換
from PIL import Image
import numpy as np
img = Image.open('01.jpg')
array = np.array(img) # PIL.Image 轉(zhuǎn) numpy
img1 = Image.fromarray(array) # numpy轉(zhuǎn) PIL.Image
img1 = Image.fromarray(array.astype('uint8'))
img1.save('from_array.jpg')更多關(guān)于Python PIL Image圖像處理基本操作實(shí)例請(qǐng)查看下面的相關(guān)鏈接
相關(guān)文章
python3注冊(cè)全局熱鍵的實(shí)現(xiàn)
這篇文章主要介紹了python3注冊(cè)全局熱鍵的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
python實(shí)現(xiàn)簡(jiǎn)單加密解密機(jī)制
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)單加密解密機(jī)制,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
Python2.7基于笛卡爾積算法實(shí)現(xiàn)N個(gè)數(shù)組的排列組合運(yùn)算示例
這篇文章主要介紹了Python2.7基于笛卡爾積算法實(shí)現(xiàn)N個(gè)數(shù)組的排列組合運(yùn)算,涉及Python笛卡爾積算法及排列組合操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-11-11
如何利用python實(shí)現(xiàn)詞頻統(tǒng)計(jì)功能
詞頻統(tǒng)計(jì)用途很廣泛,比如我們統(tǒng)計(jì)某篇文章中的用詞頻率,網(wǎng)絡(luò)熱點(diǎn)詞匯,再比如起名排行榜呀、熱門旅游景點(diǎn)排行榜呀什么的,其實(shí)也都可以套用,這篇文章主要給大家介紹了關(guān)于如何利用python實(shí)現(xiàn)詞頻統(tǒng)計(jì)功能的相關(guān)資料,需要的朋友可以參考下2021-10-10
Python3遍歷目錄樹(shù)實(shí)現(xiàn)方法
這篇文章主要介紹了Python3遍歷目錄樹(shù)實(shí)現(xiàn)方法,涉及Python目錄樹(shù)的遍歷操作技巧,需要的朋友可以參考下2015-05-05
Python實(shí)現(xiàn)處理逆波蘭表達(dá)式示例
這篇文章主要介紹了Python實(shí)現(xiàn)處理逆波蘭表達(dá)式操作,結(jié)合實(shí)例形式分析了逆波蘭表達(dá)式的概念、原理及Python針對(duì)逆波蘭表達(dá)式的定義與計(jì)算相關(guān)操作技巧,需要的朋友可以參考下2018-07-07
一篇文章告訴你如何用python進(jìn)行自動(dòng)化測(cè)試,調(diào)用c程序
這篇文章主要介紹了Python實(shí)現(xiàn)性能自動(dòng)化測(cè)試調(diào)用c程序的方法,本文圖文并茂通過(guò)實(shí)例代碼相結(jié)合的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2021-08-08

