- # conding: utf-8
- import sys,os
- import Image,ImageEnhance
- #中文字符集函数
- def encodeChinese(msg):
- type = sys.getfilesystemencoding()
- return msg.decode('UTF-8').encode(type)
- #接受亮度值函数
- def getBrightnessValue():
- print encodeChinese('请输入需要改变的亮度值:')
- brightnessValue = raw_input()
- #if True == isinstance(brightnessValue,int):
- try:
- int(brightnessValue)
- return brightnessValue
- except:
- getBrightnessValue()
- #接受饱和度值函数
- def getColorValue():
- print encodeChinese('请输入需要改变的饱和度值:')
- colorValue = raw_input()
- #if True == isinstance(brightnessValue,int):
- try:
- int(colorValue)
- return colorValue
- except:
- getColorValue()
- #设置亮度值函数
- def setBrightnessValue(brightnessValue):
- if brightnessValue != 0:
- brightnessValue = 1 + 0.01*float(brightnessValue)
- return brightnessValue
- else:
- brightnessValue = 1
- return brightnessValue
- #设置饱和度值函数
- def setColorValue(colorValue):
- if colorValue != 0:
- colorValue = 1 + 0.01*float(colorValue)
- return colorValue
- else:
- colorValue = 1
- return colorValue
- #转换亮度值函数
- def changeBrightness(img,brightnessValue):
- tmp = ImageEnhance.Brightness(img)
- img = tmp.enhance(brightnessValue)
- return img
- #转换饱和度值函数
- def changeColor(img,colorValue):
- tmp = ImageEnhance.Brightness(img)
- img = tmp.enhance(colorValue)
- return img
- #打开图形文件
- def openImageFile(filename):
- try:
- img = Image.open(filename)
- except:
- print encodeChinese('打开文件出错,这个文件是:')
- print filename
- # img.load()
- return img
- #图片另存为函数
- def ImageFileSaveAs(img,targetdir,fName):
- try:
- # tg = targetdir + os.sep + 'tmp_Image'
- # os.mkdir(tg)
- img.save(targetdir + os.sep + fName)
- except:
- os.mkdir(targetdir)
- img.save(targetdir + os.sep + fName)
- #img.save(tg + os.sep + fName)
- print str(targetdir + os.sep + fName) + '\\t' + encodeChinese('文件另存为副本完毕')
- def main():
- print encodeChinese('请输入需要处理的贴图的路径:')
- rootdir = raw_input()
- print encodeChinese('请输入保存备份贴图的路径:')
- targetdir = raw_input()
- colorValue = getColorValue()
- brightnessValue = getBrightnessValue()
- colorValue = setColorValue(colorValue)
- brightnessValue = setBrightnessValue(brightnessValue)
- for parent,dirnames,filenames in os.walk(rootdir):
- for filename in filenames:
- print 'brightnessValue: ' + str(brightnessValue)
- print 'colorValue: ' + str(colorValue)
- fName = filename
- filename = parent + os.sep + fName
- print str(filename)
- img = openImageFile(filename)
- print str(img)
- ImageFileSaveAs(img,targetdir,fName)
- #if(img.mode =='RGB'):
- img = changeBrightness(img,brightnessValue)
- img = changeColor(img,colorValue)
- img.save(filename)
- print str(filename)
- print '\\n'
- main()
- #该片段来自于http://www.codesnippet.cn/detail/160520133393.html
来源: http://www.codesnippet.cn/detail/160520133393.html