基本介紹:
Pillow 是 Python 中較為基礎的影像處理庫,主要用于影像的基本處理,比如裁剪影像、調整影像大小和影像顏色處理等,與 Pillow 相比,OpenCV 和 Scikit-image 的功能更為豐富,所以使用起來也更為復雜,主要應用于機器視覺、影像分析等領域,比如眾所周知的“人臉識別”應用 ,
Image模塊:
Image模塊是PIL最基本的模塊,其中匯出了Image類,一個Image類實體物件就對應了一副影像,同時,Image模塊還提供了很多有用的函式,
本文只是初步學習了一些用法與實際操作,
基礎函式:
| 函式名 | 簡介 |
|---|---|
| open() | 打開圖片 |
| save("test.gif","GIF") | 保存(新圖片路徑和名稱,保存格式) |
| show() | 顯示圖片 |
| size | 是img圖片物件的成員資料,是一個元組,包含了像素寬度和像素高度 |
| Image.new() | 生成新的圖片 |
| getpixel((1,1)) | 獲取像素點(1,1)三通道資訊 |
| putpixel() | 設定像素點三通道資訊 |
基礎運用:
加載圖片
from PIL import Image
pic=Image.open('test.jpg') #打開圖片:打開'test.jpg',將檔案資料返給img圖片物件
pic.show() #顯示pic中的圖片內容
x,y=pic.size #獲取圖片的像素寬度與高度
print(x*y) #獲取圖片的像素點數
遍歷img圖片的每一個像素并操作的程式框架
x,y = img.size
for i in range(x):
for j in range(y):
img圖片物件是三通道的,每個像素點包含紅、綠、藍三種通道的資訊,
img.getpixel()接受一個包含像素點坐標的元組作為傳入引數,并傳回該點的三通道值,注意傳入的是元組,所以不要漏寫函式介面括號和元組本身的括號
例如img.getpixel((12,12))就代表了(12,12)的像素點的三通道資訊
編輯圖片
pic = Image.new() 生成新的圖片物件pic(傳入兩個引數,第一個是模式字串(‘L’是8位像素黑白灰色,'RGB'是真彩色),第二個是大小元組)
pic = Image.new('L', (80, 100))
pic=Image.new("RGB",(MAX,MAX))
pic.putpixel()設定像素點的三通道資訊(傳入兩個引數,第一個是坐標元組,第二個是顏色,單通道顏色是一個數值(0是黑色),多通道顏色是一個元組)
L: pic.putpixel((i, j), 0)
RGB: pic.putpixel([x,y],(0,0,0))
實體:
0 1轉化為二維碼
from PIL import Image
MAX = 25 #數字的長度為一個整數的平方(如25^2=625,因為01的總數量為625)
pic = Image.new("RGB",(MAX, MAX))
str = "1111111000100001101111111100000101110010110100000110111010100000000010111011011101001000000001011101101110101110110100101110110000010101011011010000011111111010101010101111111000000001011101110000000011010011000001010011101101111010101001000011100000000000101000000001001001101000100111001111011100111100001110111110001100101000110011100001010100011010001111010110000010100010110000011011101100100001110011100100001011111110100000000110101001000111101111111011100001101011011100000100001100110001111010111010001101001111100001011101011000111010011100101110100100111011011000110000010110001101000110001111111011010110111011011"
i=0
for y in range(MAX):
for x in range(MAX):
if str[i]=='1':
pic.putpixel([x,y],(0,0,0))
else:
pic.putpixel([x,y],(255,255,255))
i+=1
pic.save('out.png')
像素點三通道資訊轉二維碼
同樣的思想,先轉01,再轉圖片
qr.txt部分內容
(0, 0, 0)
(0, 0, 0)
(0, 0, 0)
(255, 255, 255)
(255, 255, 255)
from PIL import Image
pixel = open('qr.txt','r').readlines();
res = "";
for i in pixel:
i = i.strip('\n') #strip()方法用于移除字串頭部、尾部指定字符
# print(i)
if i == "(255, 255, 255)":
res += '1';
elif i == "(0, 0, 0)":
res += '0';
n=0
MAX=200
pic = Image.new("RGB",(MAX, MAX))
for y in range(MAX):
for x in range(MAX):
if res[n]=='1':
pic.putpixel([x,y],(255,255,255))
else:
pic.putpixel([x,y],(0,0,0))
n+=1
pic.save('input.png','png')
結合itertools更便捷
import itertools
from PIL import Image
max = 200
file = open("qr.txt", "r")
img = Image.new("RGB", (max, max))
for y, x in itertools.product(range(max), range(max)):
pixel = eval(file.readline()) #將字串當成有效的運算式來求值,并回傳計算結果
img.putpixel([x, y], pixel)
img.save('input.png','png')
解釋:
itertools.product():以元組的形式,根據輸入的可遍歷物件生成笛卡爾積
實體:
import itertools
max=3
for x,y in itertools.product(range(max),range(max)):
print(x,y)
result:
0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/541718.html
標籤:其他
上一篇:【深入淺出 Yarn 架構與實作】4-4 RM 管理 Application
下一篇:LeetCode刷題第八九十周
