国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python實現批量按比例縮放圖片效果

2020-01-04 15:28:34
字體:
來源:轉載
供稿:網友

本文實例為大家分享了python實現批量按比例縮放圖片的具體代碼,供大家參考,具體內容如下

腳本文件放在要縮放的文件夾下面。

雙擊運行腳本,輸入要縮放的系數。腳本會在當前目錄下創建一個scaledImg_xxxx文件夾,如果已經存在,會強制刪除,如果刪除失敗會提示手動刪除這個文件夾,再雙擊運行就可以了。

resizeImg.py

#!/usr/bin/python # -*- coding:utf8 -*-  #author@skillart www.  import os import shutil import Image  to_scale = 0.5 processIndex = 0 def resizeImg(imgPath):   global processIndex   fileList = []   files = os.listdir(imgPath)   for f in files:     filePath = imgPath + os.sep + f     if(os.path.isfile(filePath)):       fileList.append(f)     elif(os.path.isdir(filePath)):       resizeImg(filePath)   for fileName in fileList:     processIndex+=1     fileFullName = imgPath+os.sep+fileName     suffix = fileName[fileName.rfind('.'):]     if(suffix == '.png' or suffix == '.jpg'):       print 'processing the '+str(processIndex)+'th file:'+fileFullName       img = Image.open(fileFullName)       w,h = img.size       tw = int(w * to_scale)       th = int(h * to_scale)       reImg = img.resize((tw,th),Image.ANTIALIAS)       reImg.save(fileFullName)       del reImg if __name__ == '__main__':   scaleStr = raw_input('input to_scale: ')   to_scale = float(scaleStr)   scaledPath = './/scaledImg_xxxx';   if os.path.isdir(scaledPath):     flag = raw_input('the output dir is exist, sure to del it(y/n)')     if flag == 'y' or flag == 'yes':       try:           shutil.rmtree(scaledPath)       finally:         raw_input('remove dir failed , please removed the dir manually.')     else:       exit   shutil.copytree('.//',scaledPath)     resizeImg(scaledPath)   raw_input("resize success") 

生成Icon

generateIcon.py

#!/usr/bin/python # -*- coding:utf8 -*-  #author@skillart www.  import os import shutil import Image  def resizeImg(imgPathName):   print imgPathName   iconDict = {'Icon.png':'72x72','Icon@2x.png':'144x144','Icon-29.png':'29x29','Icon-40.png':'40x40','Icon-50.png':'50x50',   'Icon-57.png':'57x57', 'Icon-58.png':'58x58','Icon-72.png':'72x72','Icon-76.png':'76x76','Icon-80.png':'80x80',   'Icon-100.png':'100x100','Icon-114.png':'114x114','Icon-120.png':'120x120','Icon-144.png':'144x144','Icon-152.png':'152x152',   'FlipCycleTileLarge.png':'300x300','FlipCycleTileMedium.png':'300x300','FlipCycleTileSmall.png':'300x300',   'IconicTileMediumLarge.png':'300x300','IconicTileSmall.png':'300x300','ApplicationIcon.png':'300x300','icon.png':'72x72'}   if os.path.isfile(imgPathName) == False:     print('open imgPathName failed , check the' + imgPathName + "is exist!")     exit   img = Image.open(imgPathName)   index = imgPathName.rfind(os.sep)   prefix = imgPathName[:index+1]   for key, value in iconDict.items():     # print key,value     v_split = value.split('x')     w,h = int(v_split[0]),int(v_split[1])     fileName = prefix + key     reImg = img.resize((w,h),Image.ANTIALIAS)     reImg.save(fileName)     print fileName,w,h   del img if __name__ == '__main__':   scaledPath = './/createIcon'   if os.path.isdir(scaledPath):     flag = raw_input('the output dir is exist, sure to del it(y/n)')     if flag == 'y' or flag == 'yes':       try:           shutil.rmtree(scaledPath)       finally:         raw_input('remove dir failed , please removed the dir manually.')     else:       exit   shutil.copytree('.//',scaledPath)    fileList = []   files = os.listdir(scaledPath)   for f in files:     filePath = scaledPath + os.sep + f     if os.path.isfile(filePath) :       suffix = filePath[filePath.rfind('.'):]       if(suffix == '.png' or suffix == '.jpg'):         print filePath         resizeImg(filePath)         break   raw_input("resize success") 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新兴县| 铁岭市| 汽车| 子长县| 安多县| 辽宁省| 宝坻区| 剑阁县| 南汇区| 吉隆县| 台湾省| 巴彦淖尔市| 临江市| 建宁县| 郯城县| 嘉禾县| 屯留县| 涟水县| 淮阳县| 温州市| 泽库县| 尉氏县| 盐边县| 石楼县| 石家庄市| 科技| 北辰区| 富阳市| 呼伦贝尔市| 德化县| 多伦县| 姜堰市| 潢川县| 安图县| 东乡族自治县| 宜阳县| 察雅县| 邯郸县| 汽车| 丰县| 柳州市|