python Pillow圖像處理方法匯總
這篇文章主要介紹了python Pillow圖像處理方法匯總,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
Pillow中文文檔:https://pillow-cn.readthedocs.io/zh_CN/latest/handbook/tutorial.html
安裝:pip install pillow
操作圖像:
#!/usr/bin/env python3 # _*_ coding utf-8 _*_ __author__ = 'nxz' from PIL import Image, ImageFilter from time import sleep # 打開一個jpg圖像文件 im = Image.open('test.jpg') w, h = im.size # print('圖片的寬:%s,和高:%s' % (w, h)) # 圖片縮放 im.thumbnail((w // 2, h // 2)) w, h = im.size print(w, h) # 縮放之后的圖片重新保存 im.save('thumbnail.jpg', 'jpeg') # 其他功能:切片、旋轉(zhuǎn)、濾鏡、輸出文字、調(diào)色板 # 模糊效果 im2 = im.filter(ImageFilter.BLUR) im2.save('blur.jpg','jpeg')
截屏:
from PIL import ImageGrab from time import sleep m = int(input("請輸入想截屏多少次:")) n = 1 while n <= m: sleep(0.02) im = ImageGrab.grab() local = (r'%s.jpg' % (n)) im.save(local, 'jpeg') n = n + 1
轉(zhuǎn)換文件到JPEG:
''' 將指定路徑下的圖片后綴改為 “.jpg” 格式 ''' from PIL import Image import os, sys for infile in sys.argv[1:]: f, e = os.path.splitext(infile) outfile = f + '.jpg' if infile != outfile: try: Image.open(infile).save(outfile) except Exception as exc: print(exc)
GIF動圖:
""" GIf動圖 """ from PIL import Image im = Image.open('test.jpg') images = [] images.append(Image.open('blur.png')) images.append(Image.open('test.jpg')) im.save('gif.gif', save_all=True, append_image=images, loop=1, duration=1, comment=b'aaaabbb')
幾何變換:
#簡單的集合變換 out = im.resize((128, 128)) #旋轉(zhuǎn)圖像 out = im.transpose(Image.FLIP_LEFT_RIGHT) #翻轉(zhuǎn) out = im.transpose(Image.FLIP_TOP_BOTTOM) out = im.transpose(Image.ROTATE_90) out = im.transpose(Image.ROTATE_180) #旋轉(zhuǎn)180° out = im.transpose(Image.ROTATE_270) #旋轉(zhuǎn)270°
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python中numpy數(shù)組的csv文件寫入與讀取
本文主要介紹了python中numpy數(shù)組的csv文件寫入與讀取,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03python OpenCV學(xué)習(xí)筆記之繪制直方圖的方法
本篇文章主要介紹了python OpenCV學(xué)習(xí)筆記之繪制直方圖的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02解決pycharm debug時界面下方不出現(xiàn)step等按鈕及變量值的問題
這篇文章主要介紹了解決pycharm debug時界面下方不出現(xiàn)step等按鈕及變量值的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06將pytorch轉(zhuǎn)成longtensor的簡單方法
今天小編就為大家分享一篇將pytorch轉(zhuǎn)成longtensor的簡單方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02pycharm創(chuàng)建并使用虛擬環(huán)境的詳細(xì)圖文教程
Python的虛擬環(huán)境是正常的現(xiàn)實環(huán)境相對應(yīng)的,在虛擬環(huán)境中安裝的包是與現(xiàn)實環(huán)境隔離的,下面這篇文章主要給大家介紹了關(guān)于pycharm創(chuàng)建并使用虛擬環(huán)境的詳細(xì)圖文教程,需要的朋友可以參考下2022-08-08Python 3.x基礎(chǔ)實戰(zhàn)檢查磁盤可用空間
這篇文章主要為大家介紹了Python 3.x基礎(chǔ)實戰(zhàn)之檢查磁盤可用空間實現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05詳解Python之?dāng)?shù)據(jù)序列化(json、pickle、shelve)
本篇文章主要介紹了Python之?dāng)?shù)據(jù)序列化,本節(jié)要介紹的就是Python內(nèi)置的幾個用于進(jìn)行數(shù)據(jù)序列化的模塊,有興趣的可以了解一下。2017-03-03