python實現(xiàn)批量按比例縮放圖片效果
本文實例為大家分享了python實現(xiàn)批量按比例縮放圖片的具體代碼,供大家參考,具體內(nèi)容如下
把腳本文件放在要縮放的文件夾下面。
雙擊運行腳本,輸入要縮放的系數(shù)。腳本會在當(dāng)前目錄下創(chuàng)建一個scaledImg_xxxx文件夾,如果已經(jīng)存在,會強(qiáng)制刪除,如果刪除失敗會提示手動刪除這個文件夾,再雙擊運行就可以了。
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")
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python3以GitHub為例來實現(xiàn)模擬登錄和爬取的實例講解
在本篇內(nèi)容里小編給大家分享的是關(guān)于Python3以GitHub為例來實現(xiàn)模擬登錄和爬取的實例講解,需要的朋友們可以參考下。2020-07-07
跟老齊學(xué)Python之玩轉(zhuǎn)字符串(2)更新篇
本文是玩轉(zhuǎn)字符串的續(xù)篇,繼續(xù)對字符串的連接方法進(jìn)行介紹,以及字符串復(fù)制、字符串長度、字符大小寫的轉(zhuǎn)換。非常不錯的文章,希望對大家有所幫助2014-09-09
Python實現(xiàn)批量合并Excel文件的第二張合并Excel
在數(shù)據(jù)處理和分析中,經(jīng)常需要對多個Excel文件進(jìn)行批量操作,特別是當(dāng)這些文件具有相似的結(jié)構(gòu)時,下面我們就來看看Python如何實現(xiàn)批量合并文件夾下所有Excel文件的第二張表吧2024-03-03
pandas讀取HTML和JSON數(shù)據(jù)的實現(xiàn)示例
Pandas可以直接讀取html和JSON數(shù)據(jù),本文就來介紹一下pandas讀取HTML和JSON數(shù)據(jù)的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),感興趣的可以了解一下2024-01-01
python執(zhí)行scp命令拷貝文件及文件夾到遠(yuǎn)程主機(jī)的目錄方法
今天小編就為大家分享一篇python執(zhí)行scp命令拷貝文件及文件夾到遠(yuǎn)程主機(jī)的目錄方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07

