增强亮度,,[Python]代码#
增强亮度,,[Python]代码#
[Python]代码
# -*- coding: utf-8 -*-import sys,osimport Imageimport numpyimport colorsysimport ImageEnhancecheckdir = r'c:\users\mingl_wang\desktop\lightmaps'dodir = r'c:\users\mingl_wang\desktop\test2'overdir = r'c:\users\mingl_wang\desktop\test1'def encodeChinese(msg): type = sys.getfilesystemencoding() return msg.decode('UTF-8').encode(type)#总像素数pix = 0#亮度分量总数brightValue = 0#亮度平均数reset = 0for parent,dirnames,filenames in os.walk(checkdir): for filename in filenames: #保存当前文件名 fName = filename #获取当前全路径 filename = checkdir + os.sep + filename #尝试打开文件夹当中的图片 try: img = Image.open(filename) except: print encodeChinese('打开checkdir中文件出现问题,这个图像的名字是: ') + str(filename) #在尝试将颜色通道转换成数组的时候,需要加载当前图像句柄 img.load() if img.mode == 'RGB': red,green,bule = img.split() elif img.mode == 'RGBA': red,green,bule,alpha = img.split() alphaArr = numpy.asarray(alpha) redArr = numpy.asarray(red) greenArr = numpy.asarray(green) buleArr = numpy.asarray(bule) for i in (0,img.size[0]-1): for j in (0,img.size[1]-1): if img.mode == 'RGB': item = colorsys.rgb_to_hls(redArr[j][i]/255.0,greenArr[j][i]/255.0,buleArr[j][i]/255.0) brightValue += item[1] pix += 1 if img.mode == 'RGBA': if alphaArr[j][i] < 128: continue else: item = colorsys.rgb_to_hls(redArr[j][i]/255.0,greenArr[j][i]/255.0,buleArr[j][i]/255.0) brightValue += item[1] pix += 1print encodeChinese("计算出亮度分量总数为: ") + str(brightValue)print encodeChinese("计算出像素总数为: ") + str(pix)reset = brightValue/pixprint encodeChinese("计算出平均亮度: ") + str(reset)for parent,dirnames,filenames in os.walk(dodir): for filename in filenames: #保存当前文件名 fName = filename #得到当前文件全路径 filename = dodir + os.sep + fName print encodeChinese('待处理文件路径: ') + str(filename) #打开当前图片文件 try: img = Image.open(filename) except: encodeChinese('打开处理文件时出错,这个文件的名字是: ') + str(filename) img.load() enhancer = ImageEnhance.Brightness(img) img = enhancer.enhance(40*reset) print encodeChinese('保存路径:') + str(overdir + os.sep + fName) img.save(overdir + os.sep + fName)
相关内容
- 用途都在各个函数名称之前的注释上了,用途函数注释
- Python 读取纯真IP数据库,pythonip数据库,ip.py#!/usr/
- DiscuzRobot - Discuz!论坛的机器人程序,实现了登录,签到
- 调用Windows API锁定计算机,调用windowsapi锁定,[Python]代码
- 检测图片格式,,[Python]代码#
- 金山快盤自動簽到 python 3.2,動簽,[Python]代码#!
- 使用HTML/CSS/JS开发桌面程序(基于QWebkit),cssqwebkit,s
- 从百度下载MP3歌词并把歌词设置在MP3文件中,以便在
- 最新python3 的金山快盘登录签到多功能 学习好帮手,
- 使用Python画三维函数图象,python画函数图象,[Python]代码
评论关闭