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

歡迎訪問 生活随笔!

生活随笔

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

python

Python PIL(图像处理库)使用方法

發(fā)布時間:2025/3/15 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python PIL(图像处理库)使用方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

介紹

PIL:Python Imaging Library,已經(jīng)是Python平臺事實上的圖像處理標準庫了。?
from PIL import Image, ImageDraw

安裝

pip3 install "pillow==6.2.0"

Image 對于圖像的各種操作?
ImageDraw 模塊提供了圖像對象的簡單2D繪制。用戶可以使用這個模塊創(chuàng)建新的圖像,注釋或潤飾已存在圖像,為web應(yīng)用實時產(chǎn)生各種圖形。

讀入圖像數(shù)據(jù)

Image.read(path) 讀入圖片

圖片信息

方法功能
Image.format圖像文件格式(后綴)
im.size(width,height)
Image.mode像素格式 RGB…

圖片處理

方法功能
Image.thumbnail((w//2, h//2))縮放
out = im.resize((128, 128))重定義大小
out = im.rotate(45)旋轉(zhuǎn)
im.filter(ImageFilter.BLUR)模糊
Image.show()顯示圖像(標準實現(xiàn)不是很有效率)不過測試可用
regionregion.transpose(Image.ROTATE_180)旋轉(zhuǎn)
box = (100, 100, 400, 400) region = im.crop(box)獲取子區(qū)域
Image.paste(region, box)處理完子區(qū)域粘貼回去
r, g, b = im.split()分離圖像通道
im = Image.merge(“RGB”, (b, g, r));合并圖像通道
Image.mode(‘/Users/michael/thumbnail.jpg’, ‘jpeg’)保存
//讀取圖片并顯示 from PIL import Image, ImageDraw #指定路徑 sample_image_path = os.path.join(RAW_DATA_DIR, 'normal_1/images/img_0.png') #讀入圖片 sample_image = Image.open(sample_image_path) print sample_image.format, "%dx%d" % sample_image.size, sample_image.mode #輸出 plt.title('Sample Image') plt.imshow(sample_image) plt.show() #圖片格式轉(zhuǎn)換 import os, sys import Imagefor infile in sys.argv[1:]:f, e = os.path.splitext(infile)outfile = f + ".jpg"if infile != outfile:try:Image.open(infile).save(outfile)except IOError:print "cannot convert", infile

ImageDraw

#給圖像繪制一個矩形框 sample_image_roi = sample_image.copy() fillcolor=(255,0,0) draw = ImageDraw.Draw(sample_image_roi) points = [(1,76), (1,135), (255,135), (255,76)] for i in range(0, len(points), 1):draw.line([points[i], points[(i+1)%len(points)]], fill=fillcolor, width=3) del draw #刪除變量plt.title('Image with sample ROI') plt.imshow(sample_image_roi) plt.show()

參考文檔

The Python Imaging Library Handbook

總結(jié)

以上是生活随笔為你收集整理的Python PIL(图像处理库)使用方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。