這篇文章運(yùn)用簡(jiǎn)單易懂的例子給大家介紹Python圖像處理PIL庫(kù),內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
創(chuàng)新互聯(lián)是一家專(zhuān)業(yè)提供汝陽(yáng)企業(yè)網(wǎng)站建設(shè),專(zhuān)注與成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、成都h5網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為汝陽(yáng)眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專(zhuān)業(yè)網(wǎng)絡(luò)公司優(yōu)惠進(jìn)行中。
Matlab確實(shí)是一個(gè)強(qiáng)大的圖像處理工具,不過(guò)對(duì)于一些簡(jiǎn)單的圖像處理,如圖像增強(qiáng)、圖像銳化等等,調(diào)用python的第三方包PIL(Python Image Library)也可以解決問(wèn)題,這里把python調(diào)用PIL做一些簡(jiǎn)單的圖像處理操作總結(jié)下。
主要用到PIL庫(kù)里的三個(gè)模塊:Image、ImageDraw、ImageEnhance。
1.調(diào)整圖像大小
import Image img = Image.open("1.jpg") new_img = img.resize((128,128),Image.BILINEAR) new_img.save("new_img.jpg")
將1.jpg的大小轉(zhuǎn)為128*128,Image.BILINEAR 指定采用雙線性法對(duì)像素點(diǎn)插值。
兩幅圖對(duì)比(左:原圖 右:resize后)
2.旋轉(zhuǎn)圖像
import Image img = Image.open("1.jpg") rot_img = img.rotate(45) rot_img.save("rot_img.jpg")
把圖像旋轉(zhuǎn)45度。
旋轉(zhuǎn)45度
3.格式轉(zhuǎn)換
rot_img.save("con_img.bmp")
要把上面生成的 rot_img.jpg 轉(zhuǎn)換成 bmp 圖像,加這一行即可。如果不指定保存格式,PIL 將自動(dòng)根據(jù)文件名后綴完成格式之間的轉(zhuǎn)換。
4.統(tǒng)計(jì)直方圖
import Image img = Image.open("1.jpg") new_img = img.resize((128,128),Image.BILINEAR) rot_img = new_img.rotate(45) print (rot_img.histogram())
Image 類(lèi)實(shí)例的 histogram()方法能夠?qū)χ狈綀D數(shù)據(jù)進(jìn)行統(tǒng)計(jì),并將結(jié)果做為一個(gè)列表(list)返回,將打印出所有 256 個(gè)灰度級(jí)像素點(diǎn)個(gè)數(shù)的統(tǒng)計(jì)值:
[2812, 7, 18, 18, 16, 25, 18, 22, 27, 41, 24, 34, 24, 22, 23, 20, 33, 32, 22, 20, 22, 21, 21, 24, 21, 27, 3, 8, 19, 18, 16, 14, 11, 12, 18, 17, 12, 7, 10, 13, 12, 14, 12, 11, 10, 20, 15, 6, 17, 15, 19, 7, 12, 11, 14, 11, 17, 10, 13, 8, 9, 10, 9, 10, 12, 8, 10, 12, 16, 7, 15, 6, 6, 15, 8, 11, 9, 10, 8, 11, 6, 8, 12, 7, 10, 16, 16, 7, 13, 5, 15, 15, 13, 8, 8, 13, 10, 19, 9, 13, 11, 8, 7, 18, 17, 9, 13, 10, 16, 22, 7, 17, 8, 14, 10, 11, 5, 13, 9, 17, 14, 14, 16, 15, 8, 22, 10, 16, 13, 12, 6, 5, 15, 9, 14, 9, 12, 8, 11, 9, 8, 7, 9, 16, 12, 9, 7, 14, 7, 6, 11, 16, 18, 10, 3, 16, 10, 14, 14, 14, 11, 9, 11, 12, 16, 11, 15, 9, 10, 5, 8, 8, 12, 11, 7, 15, 8, 20, 8, 9, 11, 14, 7, 19, 13, 9, 13, 7, 8, 8, 16, 20, 2, 16, 6, 13, 8, 11, 14, 14, 9, 14, 8, 14, 15, 14, 8, 14, 25, 14, 16, 21, 12, 10, 12, 17, 21, 15, 12, 34, 16, 18, 32, 34, 25, 33, 34, 37, 53, 60, 85, 97, 76, 90, 150, 155, 156, 186, 142, 188, 205, 178, 231, 203, 217, 214, 193, 342, 218, 218, 272, 439, 935, 429, 2787, 1935, 2812, 7, 29, 14, 13, 22, 19, 30, 33, 31, 30, 42, 24, 17, 22, 24, 23, 37, 18, 19, 21, 30, 20, 22, 14, 20, 4, 10, 23, 16, 18, 7, 14, 15, 15, 17, 12, 6, 23, 11, 4, 11, 13, 15, 15, 14, 17, 6, 19, 12, 19, 11, 13, 7, 12, 15, 12, 10, 11, 10, 2, 13, 11, 8, 17, 6, 15, 13, 11, 5, 9, 12, 10, 16, 16, 10, 6, 5, 6, 8, 11, 7, 14, 18, 11, 11, 15, 2, 10, 7, 16, 9, 11, 20, 4, 11, 12, 22, 4, 15, 8, 12, 9, 10, 12, 12, 14, 15, 15, 19, 11, 17, 6, 13, 6, 13, 6, 9, 12, 22, 13, 13, 11, 18, 13, 17, 8, 14, 14, 10, 14, 8, 11, 17, 10, 8, 11, 10, 11, 2, 6, 15, 4, 11, 17, 13, 12, 10, 3, 7, 17, 10, 12, 9, 11, 13, 11, 15, 14, 5, 8, 14, 8, 18, 15, 10, 8, 13, 16, 5, 10, 9, 10, 11, 10, 14, 10, 14, 7, 12, 12, 13, 7, 14, 14, 5, 18, 6, 12, 5, 13, 18, 5, 13, 10, 17, 6, 13, 20, 8, 9, 14, 14, 13, 21, 11, 12, 17, 14, 12, 8, 15, 13, 15, 12, 14, 19, 18, 17, 19, 27, 18, 23, 31, 27, 16, 27, 30, 28, 34, 37, 45, 40, 46, 64, 60, 83, 87, 113, 109, 127, 153, 192, 187, 233, 315, 362, 346, 359, 392, 920, 776, 514, 2183, 2670, 24, 2812, 1, 5, 7, 18, 30, 14, 33, 23, 33, 24, 43, 41, 27, 29, 22, 17, 29, 19, 26, 30, 22, 15, 24, 21, 22, 10, 11, 9, 19, 15, 18, 11, 16, 14, 11, 15, 7, 16, 7, 20, 11, 17, 10, 12, 16, 10, 9, 18, 12, 14, 13, 11, 12, 13, 13, 14, 16, 5, 13, 5, 7, 10, 10, 11, 15, 9, 18, 8, 12, 9, 9, 8, 9, 5, 13, 15, 7, 12, 8, 14, 4, 7, 10, 12, 22, 15, 9, 11, 5, 11, 8, 11, 14, 12, 9, 11, 19, 12, 20, 8, 4, 16, 12, 8, 15, 12, 13, 12, 16, 22, 11, 13, 10, 15, 14, 12, 12, 13, 8, 13, 16, 9, 14, 18, 14, 14, 15, 11, 13, 9, 12, 11, 11, 9, 9, 13, 12, 12, 10, 10, 9, 12, 4, 6, 7, 6, 15, 11, 11, 13, 10, 12, 12, 11, 12, 9, 15, 13, 9, 16, 9, 12, 14, 13, 9, 14, 13, 7, 7, 14, 7, 13, 6, 17, 9, 13, 10, 8, 13, 13, 6, 19, 9, 14, 9, 14, 10, 7, 9, 13, 12, 17, 11, 7, 5, 10, 23, 9, 18, 9, 15, 9, 15, 22, 7, 12, 21, 10, 20, 15, 15, 18, 20, 18, 12, 21, 11, 14, 21, 34, 22, 42, 44, 63, 48, 55, 56, 62, 87, 91, 106, 115, 164, 174, 183, 144, 159, 144, 156, 172, 190, 164, 177, 258, 229, 259, 243, 217, 288, 179, 846, 326, 316, 3907, 941]
下面的操作加入 ImageDraw 模塊,因?yàn)槔L圖操作是在圖像上進(jìn)行的,因此實(shí)例化 Draw 類(lèi)的時(shí)候要把 Image對(duì)象 img 通過(guò)參數(shù)傳遞給 Draw 類(lèi)的構(gòu)造函數(shù)。
5.繪制直線
import Image,ImageDraw img = Image.open("1.jpg") draw = ImageDraw.Draw(img) width,height = img.size # 得到img的大小 draw.line(((0,0),(width-1,height-1)),fill=255) # 對(duì)角線方向繪制直線 draw.line(((0,height-1),(width-1,0)),fill=255) img.save("cross_line.jpg")
繪制對(duì)角線
6.繪制圓
import Image,ImageDraw img = Image.open("1.jpg") width,height = img.size draw = ImageDraw.Draw(img) draw.arc((0,0,width-1,height-1),0,360,fill=255) img.save("circle.jpg")
(0,0,width-1,height-1)指定了所畫(huà)弧線的界限;0,360 是所畫(huà)弧線的起始角度和終止角度; fill=255 指定了所畫(huà)線的顏色。
繪制圓
下面的操作加入ImageEnhance 模塊,這個(gè)模塊提供了一個(gè)常用的圖像增強(qiáng)工具箱??梢杂脕?lái)進(jìn)行色彩增強(qiáng)、亮度增強(qiáng)、對(duì)比度增強(qiáng)、圖像尖銳化等等增強(qiáng)操作。所有操作都有相同形式的接口——通過(guò)相應(yīng)類(lèi)的 enhance 方法實(shí)現(xiàn)。
7.亮度增強(qiáng)
import Image,ImageEnhance img = Image.open("1.jpg") brightness = ImageEnhance.Brightness(img) bright_img = brightness.enhance(2.0) bright_img.save("bright.jpg") brightness = ImageEnhance.Brightness(img) 這一行把 img 傳給 Brightness 類(lèi),得到一個(gè) Brightness 類(lèi)實(shí)例; bright_img = brightness.enhance(2.0) 這一行調(diào)用 brightness 實(shí)例的 enhance 方法,傳入的參數(shù)指定將亮度增強(qiáng) 2 倍;
兩幅圖對(duì)比(左:原圖 右:增強(qiáng)后)
8.圖像銳化
import Image,ImageEnhance img = Image.open("1.jpg") sharpness = ImageEnhance.Sharpness(img) sharp_img = sharpness.enhance(7.0) sharp_img.save("sharp.jpg")
兩幅圖對(duì)比(左:原圖 右:銳化后)
9.對(duì)比度增強(qiáng)
import Image,ImageEnhance img = Image.open("1.jpg") contrast = ImageEnhance.Contrast(img) contrast_img = contrast.enhance(2.0) contrast_img.save("contrast.jpg")
兩幅圖對(duì)比(左:原圖 右:增強(qiáng)后)
關(guān)于Python圖像處理PIL庫(kù)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。