python pillow模塊使用方法詳解
pillow
Pillow是PIL的一個(gè)派生分支,但如今已經(jīng)發(fā)展成為比PIL本身更具活力的圖像處理庫。pillow可以說已經(jīng)取代了PIL,將其封裝成python的庫(pip即可安裝),且支持python2和python3,目前最新版本是3.0.0。
Pillow的Github主頁:https://github.com/python-pillow/Pillow
Pillow的文檔(對(duì)應(yīng)版本v3.0.0):
https://pillow.readthedocs.org/en/latest/handbook/index.html
安裝它很簡單 pip install pillow
使用方式:
#python2 import Image #python3(因?yàn)槭桥缮腜IL庫,所以要導(dǎo)入PIL中的Image) from PIL import Image
以python3為例,
open
from PIL import Image im = Image.open("1.png") im.show()
format
format屬性定義了圖像的格式,如果圖像不是從文件打開的,那么該屬性值為None;size屬性是一個(gè)tuple,表示圖像的寬和高(單位為像素);mode屬性為表示圖像的模式,常用的模式為:L為灰度圖,RGB為真彩色,CMYK為pre-press圖像。如果文件不能打開,則拋出IOError異常。
print(im.format, im.size, im.mode)
save
im.save("c:\\")
convert()
convert() 是圖像實(shí)例對(duì)象的一個(gè)方法,接受一個(gè) mode 參數(shù),用以指定一種色彩模式,mode 的取值可以是如下幾種:
· 1 (1-bit pixels, black and white, stored with one pixel per byte)
· L (8-bit pixels, black and white)
· P (8-bit pixels, mapped to any other mode using a colour palette)
· RGB (3x8-bit pixels, true colour)
· RGBA (4x8-bit pixels, true colour with transparency mask)
· CMYK (4x8-bit pixels, colour separation)
· YCbCr (3x8-bit pixels, colour video format)
· I (32-bit signed integer pixels)
· F (32-bit floating point pixels)
im = Image.open('1.png').convert('L')
Filter
from PIL import Image, ImageFilter im = Image.open(‘1.png') # 高斯模糊 im.filter(ImageFilter.GaussianBlur) # 普通模糊 im.filter(ImageFilter.BLUR) # 邊緣增強(qiáng) im.filter(ImageFilter.EDGE_ENHANCE) # 找到邊緣 im.filter(ImageFilter.FIND_EDGES) # 浮雕 im.filter(ImageFilter.EMBOSS) # 輪廓 im.filter(ImageFilter.CONTOUR) # 銳化 im.filter(ImageFilter.SHARPEN) # 平滑 im.filter(ImageFilter.SMOOTH) # 細(xì)節(jié) im.filter(ImageFilter.DETAIL)
查看圖像直方圖
im.histogram()
轉(zhuǎn)換圖像文件格式
def img2jpg(imgFile): if type(imgFile)==str and imgFile.endswith(('.bmp', '.gif', '.png')): with Image.open(imgFile) as im: im.convert('RGB').save(imgFile[:-3]+'jpg') img2jpg('1.gif') img2jpg('1.bmp') img2jpg('1.png')
屏幕截圖
from PIL import ImageGrab im = ImageGrab.grab((0,0,800,200)) #截取屏幕指定區(qū)域的圖像 im = ImageGrab.grab() #不帶參數(shù)表示全屏幕截圖
圖像裁剪與粘貼
box = (120, 194, 220, 294) #定義裁剪區(qū)域 region = im.crop(box) #裁剪 region = region.transpose(Image.ROTATE_180) im.paste(region,box) #粘貼
圖像縮放
im = im.resize((100,100)) #參數(shù)表示圖像的新尺寸,分別表示寬度和高度
圖像對(duì)比度增強(qiáng)
from PIL import Image from PIL import ImageEnhance #原始圖像 image = Image.open('lena.jpg') image.show() #亮度增強(qiáng) enh_bri = ImageEnhance.Brightness(image) brightness = 1.5 image_brightened = enh_bri.enhance(brightness) image_brightened.show() #色度增強(qiáng) enh_col = ImageEnhance.Color(image) color = 1.5 image_colored = enh_col.enhance(color) image_colored.show() #對(duì)比度增強(qiáng) enh_con = ImageEnhance.Contrast(image) contrast = 1.5 image_contrasted = enh_con.enhance(contrast) image_contrasted.show() #銳度增強(qiáng) enh_sha = ImageEnhance.Sharpness(image) sharpness = 3.0 image_sharped = enh_sha.enhance(sharpness) image_sharped.show()
Image模塊用法介紹
1. 簡介。
圖像處理是一門應(yīng)用非常廣的技術(shù),而擁有非常豐富第三方擴(kuò)展庫的 Python 當(dāng)然不會(huì)錯(cuò)過這一門盛宴。PIL (Python Imaging Library)是 Python 中最常用的圖像處理庫,目前版本為 1.1.7,我們可以 在這里 下載學(xué)習(xí)和查找資料。
Image 類是 PIL 庫中一個(gè)非常重要的類,通過這個(gè)類來創(chuàng)建實(shí)例可以有直接載入圖像文件,讀取處理過的圖像和通過抓取的方法得到的圖像這三種方法。
2. 使用。
導(dǎo)入 Image 模塊。然后通過 Image 類中的 open 方法即可載入一個(gè)圖像文件。如果載入文件失敗,則會(huì)引起一個(gè) IOError ;若無返回錯(cuò)誤,則 open 函數(shù)返回一個(gè) Image 對(duì)象?,F(xiàn)在,我們可以通過一些對(duì)象屬性來檢查文件內(nèi)容,即:
>>> import Image >>> im = Image.open("j.jpg")>>> im.show() >>> print im.format, im.size, im.mode JPEG (440, 330) RGB
這里有三個(gè)屬性,我們逐一了解。
format : 識(shí)別圖像的源格式,如果該文件不是從文件中讀取的,則被置為 None 值。
size : 返回的一個(gè)元組,有兩個(gè)元素,其值為象素意義上的寬和高。
mode : RGB(true color image),此外還有,L(luminance),CMTK(pre-press image)。
from PIL import Image from PIL import ImageEnhance import pytesseract import re pytesseract.pytesseract.tesseract_cmd = 'D:\\Program Files\\Tesseract-OCR\\tesseract.exe' tessdata_dir_config = '--tessdata-dir "D:\\Program Files\\Tesseract-OCR\\tessdata"' im=Image.open("./img/10.jpg") im=im.convert('L') im.show() im=ImageEnhance.Contrast(im) im=im.enhance(1) #im = im.resize((300, 90)) ltext = pytesseract.image_to_string(im) ltext = re.sub("\W", "", ltext) im.show() print(ltext) #print(pytesseract.image_to_string(im)) #print(pytesseract.image_to_boxes(im)) #print(im.format, im.size, im.mode)
convert() : 該函數(shù)可以用來將圖像轉(zhuǎn)換為不同色彩模式。
ImageEnhance.Contrast(im):使用ImageEnhance可以增強(qiáng)圖片的識(shí)別率
其他
簡單的幾何變換。
>>>out = im.resize((128, 128)) #調(diào)整圖片大小 >>>out = im.rotate(45) #逆時(shí)針旋轉(zhuǎn) 45 度角。 >>>out = im.transpose(Image.FLIP_LEFT_RIGHT) #左右對(duì)換。 >>>out = im.transpose(Image.FLIP_TOP_BOTTOM) #上下對(duì)換。 >>>out = im.transpose(Image.ROTATE_90) #旋轉(zhuǎn) 90 度角。 >>>out = im.transpose(Image.ROTATE_180) #旋轉(zhuǎn) 180 度角。 >>>out = im.transpose(Image.ROTATE_270) #旋轉(zhuǎn) 270 度角。
序列圖像。
即我們常見到的動(dòng)態(tài)圖,最常見的后綴為 .gif ,另外還有 FLI / FLC 。PIL 庫對(duì)這種動(dòng)畫格式圖也提供了一些基本的支持。當(dāng)我們打開這類圖像文件時(shí),PIL 自動(dòng)載入圖像的第一幀。我們可以使用 seek 和 tell 方法在各幀之間移動(dòng)。
import Image im.seek(1) # skip to the second frame try: while 1: im.seek( im.tell() + 1) # do something to im except EOFError: pass
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
深入理解python?生成器、迭代器、動(dòng)態(tài)新增屬性及方法
這篇文章主要介紹了python?生成器、迭代器、動(dòng)態(tài)新增屬性及方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04Windows自動(dòng)執(zhí)行python腳本操作步驟
我們想在Windows中運(yùn)行一個(gè)Python腳本,我們可以通過CMD,首先進(jìn)入python文件所在的目錄,之后運(yùn)行。但是這樣很麻煩,跟著本文操作就可以解決啦2021-09-09python-str,list,set間的轉(zhuǎn)換實(shí)例
今天小編就為大家分享一篇python-str,list,set間的轉(zhuǎn)換實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06Python實(shí)現(xiàn)統(tǒng)計(jì)圖像連通域的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)統(tǒng)計(jì)圖像連通域的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-04-04