您好,登錄后才能下訂單哦!
這篇文章運用簡單易懂的例子給大家介紹Python圖像處理PIL庫,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
Matlab確實是一個強大的圖像處理工具,不過對于一些簡單的圖像處理,如圖像增強、圖像銳化等等,調用python的第三方包PIL(Python Image Library)也可以解決問題,這里把python調用PIL做一些簡單的圖像處理操作總結下。
主要用到PIL庫里的三個模塊:Image、ImageDraw、ImageEnhance。
1.調整圖像大小
import Image img = Image.open("1.jpg") new_img = img.resize((128,128),Image.BILINEAR) new_img.save("new_img.jpg")
將1.jpg的大小轉為128*128,Image.BILINEAR 指定采用雙線性法對像素點插值。
兩幅圖對比(左:原圖 右:resize后)
2.旋轉圖像
import Image img = Image.open("1.jpg") rot_img = img.rotate(45) rot_img.save("rot_img.jpg")
把圖像旋轉45度。
旋轉45度
3.格式轉換
rot_img.save("con_img.bmp")
要把上面生成的 rot_img.jpg 轉換成 bmp 圖像,加這一行即可。如果不指定保存格式,PIL 將自動根據文件名后綴完成格式之間的轉換。
4.統計直方圖
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 類實例的 histogram()方法能夠對直方圖數據進行統計,并將結果做為一個列表(list)返回,將打印出所有 256 個灰度級像素點個數的統計值:
[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 模塊,因為繪圖操作是在圖像上進行的,因此實例化 Draw 類的時候要把 Image對象 img 通過參數傳遞給 Draw 類的構造函數。
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) # 對角線方向繪制直線 draw.line(((0,height-1),(width-1,0)),fill=255) img.save("cross_line.jpg")
繪制對角線
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)指定了所畫弧線的界限;0,360 是所畫弧線的起始角度和終止角度; fill=255 指定了所畫線的顏色。
繪制圓
下面的操作加入ImageEnhance 模塊,這個模塊提供了一個常用的圖像增強工具箱。可以用來進行色彩增強、亮度增強、對比度增強、圖像尖銳化等等增強操作。所有操作都有相同形式的接口——通過相應類的 enhance 方法實現。
7.亮度增強
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 類,得到一個 Brightness 類實例; bright_img = brightness.enhance(2.0) 這一行調用 brightness 實例的 enhance 方法,傳入的參數指定將亮度增強 2 倍;
兩幅圖對比(左:原圖 右:增強后)
8.圖像銳化
import Image,ImageEnhance img = Image.open("1.jpg") sharpness = ImageEnhance.Sharpness(img) sharp_img = sharpness.enhance(7.0) sharp_img.save("sharp.jpg")
兩幅圖對比(左:原圖 右:銳化后)
9.對比度增強
import Image,ImageEnhance img = Image.open("1.jpg") contrast = ImageEnhance.Contrast(img) contrast_img = contrast.enhance(2.0) contrast_img.save("contrast.jpg")
兩幅圖對比(左:原圖 右:增強后)
關于Python圖像處理PIL庫就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。