最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python PIL Image 圖像處理基本操作實例

 更新時間:2022年04月08日 16:37:23   作者:-牧野-  
這篇文章主要介紹了python PIL Image 圖像處理基本操作實例包括圖片加載、灰度圖,圖像通道分離和合并,在圖像上輸出文字,圖像縮放,圖像閾值分割、 二值化,圖像裁剪需要的朋友可以參考下

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)建指定大小,指定通道類型的空圖像

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. 訪問和操作圖像像素

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對象:
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

# 前兩個坐標(biāo)點是左上角坐標(biāo)
# 后兩個坐標(biāo)點是右下角坐標(biāo)
# width在前, height在后
box = (100, 100, 550, 350)

region = img.crop(box)

region.save('crop.jpg')

11. 圖像邊界擴展

# 邊界擴展
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))

# 圖像水平擴展整個圖像
img_makeBorder_full.paste(img, (0, 0, width, height))
img_makeBorder_full.paste(img, (width, 0, 2*width, height))

# 前兩個坐標(biāo)點是左上角坐標(biāo)
# 后兩個坐標(biāo)點是右下角坐標(biāo)
# width在前, height在后
box = (width-200, 0, width, height)
region = img.crop(box)

# 圖像水平右側(cè)擴展一個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圖像處理基本操作實例請查看下面的相關(guān)鏈接

相關(guān)文章

  • Python中的“_args”和“__kwargs”用法詳解

    Python中的“_args”和“__kwargs”用法詳解

    *args 和 **kwargs 主要?于函數(shù)定義,你可以將不定數(shù)量的參數(shù)傳遞給?個函數(shù),這篇文章主要介紹了Python中的“_args”和“__kwargs”用法,需要的朋友可以參考下
    2023-01-01
  • Python 包含漢字的文件讀寫之每行末尾加上特定字符

    Python 包含漢字的文件讀寫之每行末尾加上特定字符

    這篇文章主要介紹了Python 包含漢字的文件讀寫之每行末尾加上特定字符的相關(guān)資料,需非常不錯,具有參考借鑒價值,要的朋友可以參考下
    2016-12-12
  • Python中set與frozenset方法和區(qū)別詳解

    Python中set與frozenset方法和區(qū)別詳解

    這篇文章主要介紹了Python中set與frozenset方法和區(qū)別詳解的相關(guān)資料,需要的朋友可以參考下
    2016-05-05
  • python密碼學(xué)Base64編碼和解碼教程

    python密碼學(xué)Base64編碼和解碼教程

    這篇文章主要為大家介紹了python密碼學(xué)Base64編碼和解碼教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • python繪制封閉多邊形教程

    python繪制封閉多邊形教程

    今天小編就為大家分享一篇python繪制封閉多邊形教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • 神經(jīng)網(wǎng)絡(luò)訓(xùn)練采用gpu設(shè)置的方式

    神經(jīng)網(wǎng)絡(luò)訓(xùn)練采用gpu設(shè)置的方式

    這篇文章主要介紹了神經(jīng)網(wǎng)絡(luò)訓(xùn)練采用gpu設(shè)置的方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • Python處理PPT文件的實用知識點總結(jié)

    Python處理PPT文件的實用知識點總結(jié)

    python是一門很強大的語言,因為有著豐富的第三方庫,所以可以說Python是無所不能的,下面這篇文章主要給大家介紹了關(guān)于用Python提取PPT中圖片的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-01-01
  • Python+Tkinter制作在線個性簽名工具

    Python+Tkinter制作在線個性簽名工具

    這篇文章主要為大家分享如何利用Python中的Tkinter庫制作一個簡易的在線個性簽名生成工具,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
    2022-03-03
  • numpy庫reshape用法詳解

    numpy庫reshape用法詳解

    這篇文章主要介紹了numpy庫reshape用法詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Python中對象的引用與復(fù)制的使用

    Python中對象的引用與復(fù)制的使用

    引用和復(fù)制是Python處理對象的重要概念,本文主要介紹了Python中對象的引用與復(fù)制的使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-12-12

最新評論

页游| 木兰县| 富川| 清徐县| 睢宁县| 库伦旗| 喀喇沁旗| 黑龙江省| 鄢陵县| 岱山县| 西丰县| 甘泉县| 潜江市| 吴桥县| 新龙县| 桐城市| 哈密市| 南阳市| 固安县| 安乡县| 邳州市| 康马县| 绥化市| 宁南县| 通化县| 璧山县| 阿拉善右旗| 高密市| 河北区| 卢湾区| 赤水市| 邢台市| 进贤县| 绵竹市| 贺兰县| 钟山县| 临江市| 永康市| 桐柏县| 沐川县| 宜川县|