您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)python如何實現(xiàn)批量按比例縮放圖片效果的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
把腳本文件放在要縮放的文件夾下面。
雙擊運(yùn)行腳本,輸入要縮放的系數(shù)。腳本會在當(dāng)前目錄下創(chuàng)建一個scaledImg_xxxx文件夾,如果已經(jīng)存在,會強(qiáng)制刪除,如果刪除失敗會提示手動刪除這個文件夾,再雙擊運(yùn)行就可以了。
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")
感謝各位的閱讀!關(guān)于“python如何實現(xiàn)批量按比例縮放圖片效果”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。