【Python】pillow庫(圖像處理)

pillow

官方文檔:https://pillow.readthedocs.io/en/latest/handbook/tutorial.html

讀寫圖像

—獲取圖像的基本信息

from PIL import Image
image=Image.open('1.jpg')#讀取圖像
#圖像的基本信息
print(image.info)        #圖像的二進制數據
print(image.size)        #圖像的尺寸
print(image.format)      #圖像的格式
print(image.mode)        #圖像的格式
print(dir(image))        #查看Image實例對象的屬性

Image對象實例的屬性
im.format:標識了圖像來源。如果圖像不是從文件讀取它的值就是None
im.size:是一個二元tuple,包含width和height(寬度和高度,單位都是px) im.mode:常見的modes 有 “L” (luminance) 表示灰度圖像, “RGB” 表示真彩色圖像, “CMYK” 表示出版圖像

—將圖片轉換爲JPEG格式

from PIL import Image
Image.open('1.jpg').save('2.jpg')

—創建JPEG略縮圖

from PIL import Image
im_size=(80,80)
im=Image.open('1.jpg')
print(im.format,im.size,im.mode)
im.thumbnail(im_size)
im.save('newpic.JPEG','JPEG')#保存圖片

im.save(file_name,pic_format)的參數
file_name:[string]
pic_format:[string] 可選參數"JPG",“JPEG”,“PNG”

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章