python實現(xiàn)批量按比例縮放圖片效果
更新時間:2018年03月30日 16:00:49 作者:skillart
這篇文章主要為大家詳細介紹了python實現(xiàn)批量按比例縮放圖片效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python實現(xiàn)批量按比例縮放圖片的具體代碼,供大家參考,具體內容如下
把腳本文件放在要縮放的文件夾下面。
雙擊運行腳本,輸入要縮放的系數(shù)。腳本會在當前目錄下創(chuàng)建一個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")
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python3以GitHub為例來實現(xiàn)模擬登錄和爬取的實例講解
在本篇內容里小編給大家分享的是關于Python3以GitHub為例來實現(xiàn)模擬登錄和爬取的實例講解,需要的朋友們可以參考下。2020-07-07Python實現(xiàn)批量合并Excel文件的第二張合并Excel
在數(shù)據(jù)處理和分析中,經常需要對多個Excel文件進行批量操作,特別是當這些文件具有相似的結構時,下面我們就來看看Python如何實現(xiàn)批量合并文件夾下所有Excel文件的第二張表吧2024-03-03pandas讀取HTML和JSON數(shù)據(jù)的實現(xiàn)示例
Pandas可以直接讀取html和JSON數(shù)據(jù),本文就來介紹一下pandas讀取HTML和JSON數(shù)據(jù)的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,感興趣的可以了解一下2024-01-01python執(zhí)行scp命令拷貝文件及文件夾到遠程主機的目錄方法
今天小編就為大家分享一篇python執(zhí)行scp命令拷貝文件及文件夾到遠程主機的目錄方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07