網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
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. 訪問和操作圖像像素
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')
原文鏈接:https://blog.csdn.net/dcrmg/article/details/102963336?spm=1001.2014.3001.5502
相關(guān)推薦
- 2023-04-27 c++中關(guān)于max_element()函數(shù)解讀_C 語(yǔ)言
- 2022-05-18 python操作jira添加模塊的方法_python
- 2023-02-18 Framework中實(shí)現(xiàn)OC和Swift的混編方案_IOS
- 2022-09-04 django連接數(shù)據(jù)庫(kù)獲取數(shù)據(jù)的簡(jiǎn)單步驟記錄_python
- 2022-10-16 go?swagger生成接口文檔使用教程_Golang
- 2022-04-24 Pygame?Surface創(chuàng)建圖像的實(shí)現(xiàn)_python
- 2022-07-06 C#中的反射(System.Reflection)_C#教程
- 2022-04-24 python使用技巧-標(biāo)準(zhǔn)輸入_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支