日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python之PIL库

發(fā)布時間:2024/4/15 python 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python之PIL库 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Python PIL?

? ? ? ? PIL (Python Image Library)?庫是Python?語言的一個第三方庫,PIL庫支持圖像存儲、顯示和處理,能夠處理幾乎所有格式的圖片。

一、PIL庫簡介

1. PIL庫主要有2個方面的功能:

(1)?圖像歸檔:對圖像進行批處理、生產圖像預覽、圖像格式轉換等。

(2)?圖像處理:圖像基本處理、像素處理、顏色處理等。

二、安裝庫函數

pip install pillow

三、使用庫函數Image類 —— 基本圖像處理

1. 調用Image類

from PIL import Image

?

2. Image類關于圖像讀取與創(chuàng)建

方法

說明

Image.open(filename)

加載圖像文件

Image.new(mode,size,color)

根據給定參數創(chuàng)建新圖像

Image.open(StringIO.StringIO(buffer))

從字符串中獲取圖像

Image.frombytes(mode,size,color)

根據像素點創(chuàng)建新圖像

Image.verify()

對圖像完整性進行檢查,返回異常

?

?

3.?Image類的常用屬性

方法

說明

Image.format

圖像格式或來源,若圖像不是從文件讀取,返回None

Image.mode

圖像的色彩模式,’L’為灰度模式,’RGB’為真彩色圖像,’C(青)M(品紅)Y(黃)K(黑)’為出版圖像

Image.size

圖像的寬度和高度,單位是像素(px),返回值為元組類型

Image.palette

調色板屬性,返回ImagePalette類型

?

4.Image類的序列圖像操作方法

?

方法

說明

Image.seek(frame)

跳轉并返回圖像中的指定幀

Image.tell()

返回當前幀的序號

?

5.Image類的圖像旋轉和縮放方法

方法

說明

Image.resize(size)

返回按size大小調整圖像的副本

Image.rotate(angle)

返回按angle角度旋轉圖像的副本

6.Image類的圖像轉換和保存方法

方法

說明

Image.save(filename,format)

將圖像保存為filename文件名,format格式

Image.convert(mode)

將圖像轉換為mode模式

Image.thumbnail(size)

創(chuàng)建圖像的縮略圖,size是縮略圖尺寸的元組

?

?例子1

# -*- encoding:utf-8 -*- ''' 改變顏色 --- 顏色反轉''' from PIL import Image nest = Image.open("D:\\nest.jpg") r,g,b = nest.split() #獲取原圖的RGB通道的顏色值 newb = b.point(lambda i:i*1.1) #將B通道的顏色值增強 nest1 = Image.merge(nest.mode,(b,g,r)) nest1.thumbnail((400,254)) #創(chuàng)建縮略圖 nest1.save("D:\\nest_2.jpg")

  

處理圖像序列(gif圖像)

函數:ImageSequence()

下面的代碼可以遍歷gif圖像中的所有幀,并分別保存為圖像。

index = 0 while 1:try:gif.seek(index)gif.save("%d.%s" %(index,'jpg' if gif.mode == 'JPEG' else 'png'))index += 1... except EOFError:print("Reach the end of gif sequence!")break

ImageEnhance()

from PIL import Image , ImageEnhanceim = Image.open("F:\\test\\image1\\person.jpg")brightness = ImageEnhance.Brightness(im)im_brightness = brightness.enhance(1.5)contrast = ImageEnhance.Contrast(im)im_contrast = contrast.enhance(1.5)im_brightness.save("./image1/im_brightness.jpg")im_contrast.save("./image1/im_contrast.jpg")

ImageEnhance是PIL下的一個子類,主要用于圖像增強,比如增加亮度(Brightness),增加對比度(Contrast)等。上面的代碼將原來圖像的亮度增加50%,將對比度也增加了50%。

接下來把圖片變成素描的效果

from PIL import Imagedef sketch(img, threshold):'''素描param img: Image實例param threshold: 介于0到100'''if threshold < 0: threshold = 0if threshold > 100: threshold = 100width, height = img.sizeimg = img.convert('L') # convert to grayscale modepix = img.load() # get pixel matrixfor w in xrange(width):for h in xrange(height):if w == width-1 or h == height-1:continuesrc = pix[w, h]dst = pix[w+1, h+1]diff = abs(src - dst)if diff >= threshold:pix[w, h] = 0else:pix[w, h] = 255return img if __name__ == "__main__":import sys, ospath = os.path.dirname(__file__) + os.sep.join(['', 'images', 'lam.jpg'])threshold = 15if len(sys.argv) == 2:try:threshold = int(sys.argv[1])except ValueError:path = sys.argv[1]elif len(sys.argv) == 3:path = sys.argv[1]threshold = int(sys.argv[2])img = Image.open(path)img = sketch(img, threshold)img.save(os.path.splitext(path)[0]+'.sketch.jpg', 'JPEG')

效果對比圖。

轉載于:https://www.cnblogs.com/gyy-15768200938/p/10686756.html

總結

以上是生活随笔為你收集整理的Python之PIL库的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。