python增加图像对比度的方法


Posted in Python onJuly 12, 2019

本代码实现的是,在旋转10度的基础上,再进行增加对比度的操作。

1 代码:

代码注释中的代码都是可以运行的.  但是不怎么靠谱,因为文件名被逐个编辑,有可能与原标签不对应,,更好的做法参考代码2

# -*- coding: UTF-8 -*-
from PIL import Image
from PIL import ImageEnhance
import PIL.Image as img
from PIL import ImageEnhance
import os
 
def rotationImage(filepath,destpath):
 count = 0
 filelist=os.listdir(filepath) #所有文件的文件名
 total_num=len(filelist) #所有文件的个数
 print(total_num) #输出文件个数
 for i in range(total_num): #对每张图像进行操作
  print(count)
  im=img.open(filepath+str(i+21)+str("_training")+".gif")
  for j in range(72):
   im_rotate=im.rotate(j*10) #每张图像都10°旋转一次
   #然后对其增加亮度对比度等操作
 
   enh_con=ImageEnhance.Contrast(im_rotate) #增加对比度 得到1440张
   image_contrasted=enh_con.enhance(1.5)
   image_contrasted.save(destpath + str("cont_") + str((j + 1) * 10) + str("_") + str(i + 21) + str("_") + str("manual1") + '.gif')
   count=count+1
   # enh_sha=ImageEnhance.Sharpness(im_rotate) #增加锐度
   # image_sharped=enh_sha.enhance(3.0)
   # image_sharped.save(destpath + str("sharp_") + str((j + 1) * 10) + str("_") + str(i + 21) + str("_") + str("training") + '.tif')
 
   # enh_bri=ImageEnhance.Brightness(im_rotate) #增加亮度 但是有问题
   # image_bright=enh_bri.enhance(1.5)
   # image_bright.save(destpath + str("bri_") + str((j + 1) * 10) + str("_") + str(i + 21) + str("_") + str("training") + '.tif')
 
   # enh_col=ImageEnhance.Color(im_rotate) #增加色度 但是有问题,
   # image_colored=enh_col.enhance(1.5)
   # image_colored.save(destpath + str("col_") + str((j + 1) * 10) + str("_") + str(i + 21) + str("_") + str("training") + '.tif')
 
  j=0
 
if __name__== '__main__':
 filepath='/home/qxq/Desktop/eyedata_final/train/label/gif/orginal/'
 destpath='/home/qxq/Desktop/eyedata_final/train/label/gif/brighten/'
 rotationImage(filepath,destpath)

2 代码:

更加靠谱的做法如下:

# -*- coding: UTF-8 -*-
from PIL import Image
from PIL import ImageEnhance
import os
 
rootdir = r'/home/qxq/Desktop/eyedata_final/mask/original/' # 指明被遍历的文件夹
for parent, dirnames, filenames in os.walk(rootdir):
 for filename in filenames:
  currentPath = os.path.join(parent, filename)
  im = Image.open(currentPath)
  for j in range(72):
   im_rotate = im.rotate(j * 10) # 每张图像都10°旋转一次
 
   enh_con = ImageEnhance.Contrast(im_rotate) # 增加对比度 得到1440张(20*72=1440)
   image_contrasted = enh_con.enhance(1.5)
   newname1 = r"/home/qxq/Desktop/eyedata_final/mask/brighten/" + 'Cont_' + filename
   image_contrasted.save(newname1)
 
   enh_sha = ImageEnhance.Sharpness(im_rotate) # 增加锐度
   image_sharped = enh_sha.enhance(3.0)
   newname2 = r"/home/qxq/Desktop/eyedata_final/mask/brighten/" + 'sharp_' + filename
   image_contrasted.save(newname2)
 
   #
   enh_bri = ImageEnhance.Brightness(im_rotate) # 增加亮度 但是有问题
   image_bright = enh_bri.enhance(1.5)
   newname3 = r"/home/qxq/Desktop/eyedata_final/mask/brighten/" + 'Bri_' + filename
   image_contrasted.save(newname3)
 
   #
   enh_col = ImageEnhance.Color(im_rotate) # 增加色度 但是有问题,
   image_colored = enh_col.enhance(1.5)
   newname4 = r"/home/qxq/Desktop/eyedata_final/mask/brighten/" + 'Col_' + filename
   image_contrasted.save(newname4)
 
 
  j = 0

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python生成随机密码
Mar 10 Python
django之常用命令详解
Jun 30 Python
基于ID3决策树算法的实现(Python版)
May 31 Python
Ubuntu 下 vim 搭建python 环境 配置
Jun 12 Python
Python3实现简单可学习的手写体识别(实例讲解)
Oct 21 Python
Python读取Excel表格,并同时画折线图和柱状图的方法
Oct 14 Python
利用Python+阿里云实现DDNS动态域名解析的方法
Apr 01 Python
Python内置加密模块用法解析
Nov 25 Python
Ubuntu18.04安装 PyCharm并使用 Anaconda 管理的Python环境
Apr 08 Python
vscode写python时的代码错误提醒和自动格式化的方法
May 07 Python
Python日志打印里logging.getLogger源码分析详解
Jan 17 Python
Python中的tkinter库简单案例详解
Jan 22 Python
Python 控制终端输出文字的实例
Jul 12 #Python
在Django的View中使用asyncio的方法
Jul 12 #Python
检测python爬虫时是否代理ip伪装成功的方法
Jul 12 #Python
在PyCharm中控制台输出日志分层级分颜色显示的方法
Jul 11 #Python
基于sklearn实现Bagging算法(python)
Jul 11 #Python
Python的log日志功能及设置方法
Jul 11 #Python
python使用装饰器作日志处理的方法
Jul 11 #Python
You might like
php $_SERVER["REQUEST_URI"]获取值的通用解决方法
2010/06/21 PHP
浅析PHP原理之变量分离/引用(Variables Separation)
2013/08/09 PHP
PHP连接MySQL的2种方法小结以及防止乱码
2014/03/11 PHP
php上传功能集后缀名判断和随机命名(强力推荐)
2015/09/10 PHP
php nginx 实时输出的简单实现方法
2018/01/21 PHP
PHP合并两个或多个数组的方法
2019/01/20 PHP
js列举css中所有图标的实现代码
2011/07/04 Javascript
Google (Local) Search API的简单使用介绍
2013/11/28 Javascript
浅析JavaScript中两种类型的全局对象/函数
2013/12/05 Javascript
jQuery中outerWidth()方法用法实例
2015/01/19 Javascript
深入理解setTimeout函数和setInterval函数
2016/05/20 Javascript
基于Bootstrap的UI扩展 StyleBootstrap
2016/06/17 Javascript
jquery实现拖动效果
2016/08/10 Javascript
关于angularJs指令的Scope(作用域)介绍
2016/10/25 Javascript
javascript滚轮事件基础实例讲解(37)
2017/02/14 Javascript
基于匀速运动的实例讲解(侧边栏,淡入淡出)
2017/10/17 Javascript
详解vue 数组和对象渲染问题
2018/09/21 Javascript
ES6基础之解构赋值(destructuring assignment)
2019/02/21 Javascript
基于better-scroll 实现歌词联动功能的代码
2020/05/07 Javascript
Python爬虫番外篇之Cookie和Session详解
2017/12/27 Python
教你用 Python 实现微信跳一跳(Mac+iOS版)
2018/01/04 Python
Python实现的括号匹配判断功能示例
2018/08/25 Python
python实现植物大战僵尸游戏实例代码
2019/06/10 Python
Python类反射机制使用实例解析
2019/12/30 Python
解决Jupyter Notebook使用parser.parse_args出现错误问题
2020/04/20 Python
matplotlib基础绘图命令之bar的使用方法
2020/08/13 Python
python安装及变量名介绍详解
2020/12/12 Python
详解Python爬虫爬取博客园问题列表所有的问题
2021/01/18 Python
中国领先的专业演出票务网:永乐票务
2016/08/29 全球购物
英国地毯卖家:The Rug Seller
2019/07/18 全球购物
override和overload的区别
2016/03/09 面试题
上课看小说检讨书
2014/02/22 职场文书
铲车司机岗位职责
2014/03/15 职场文书
2015年全国爱眼日活动方案
2015/05/05 职场文书
2015年幼儿园班主任工作总结
2015/05/12 职场文书
pycharm 如何查看某一函数源码的快捷键
2021/05/12 Python