日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

[ Python ] PIL

發布時間:2025/5/22 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [ 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的全部內容,希望文章能夠幫你解決所遇到的問題。

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