[ Python ] PIL
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                [ 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的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 和在电脑上怎么打开不了怎么办 电脑打不开
 - 下一篇: 怎么把win10系统换成win7系统软件