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

歡迎訪問 生活随笔!

生活随笔

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

python

[ Python ] PIL

發布時間:2025/5/22 python 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [ Python ] PIL 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

示例代碼

# coding: utf-8from PIL import Image import os, sysInt_Len = 5def getPicInfo(pic):im = Image.open(pic)print("--------------------")print("file name:\t" + im.filename)print("format:\t\t" + im.format)print("size:\t\t" + str(im.size))print("mode:\t\t" + im.mode)return imdef convertFormat(pic, format):pathNew = getNewFormatPath(pic, format)try:im = Image.open(pic)im.save(pathNew, format)getPicInfo(pathNew)return pathNewexcept IOError:print("Cannot create new Format %s for %s." % format, pic)return Nonedef convertThumbnails(pic, rate=1, format='JPEG'):pathNew = getThumbnailsPath(pic, r"thumbnails", format)if (pic != pathNew):try:im = Image.open(pic)size = (int(im.size[0] * rate), int(im.size[1] * rate))im.thumbnail(size)im.save(pathNew, format)getPicInfo(pathNew)return pathNewexcept IOError:print("Cannot create thumbnail for ", pic)return Nonedef getNewFormatPath(pic, format):pathSegs = os.path.splitext(pic)count = 1while True:pathNew = pathSegs[0] + r"-" + str(count).zfill(Int_Len) + "." + format.lower()if os.path.exists(pathNew):count += 1pathNew = pathSegs[0] + r"-" + str(count).zfill(Int_Len) + "." + format.lower()else:return pathNewdef getThumbnailsPath(pic, suffix, format):pathSegs = os.path.splitext(pic)count = 1while True:pathNew = pathSegs[0] + r"-" + str(count).zfill(Int_Len) + r"-" + suffix + "." + format.lower()if os.path.exists(pathNew):count += 1pathNew = pathSegs[0] + r"-" + str(count).zfill(Int_Len) + r"-" + suffix + "." + format.lower()else:return pathNewif __name__ == "__main__":pic1 = r"res/MIT-001.jpg"pic2 = r"res/MIT-002.jpg"# im = getPicInfo(pic1)# r,g,b = im.split()# r.rotate(45).show()# g.resize((100,200)).show()# b.show()# im.convert("L").convert("RGB").show()# convertThumbnails(pic1, 0.5)path = convertFormat(pic1, 'PNG')print(os.path.abspath(path))

  

轉載于:https://www.cnblogs.com/coder211/p/9055969.html

總結

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

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