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中字典映射类型的学习教程
Aug 20 Python
Python如何判断数独是否合法
Sep 08 Python
python爬取亚马逊书籍信息代码分享
Dec 09 Python
python机器学习理论与实战(二)决策树
Jan 19 Python
python中的for循环
Sep 28 Python
Python 中的参数传递、返回值、浅拷贝、深拷贝
Jun 25 Python
Python通过VGG16模型实现图像风格转换操作详解
Jan 16 Python
Python基础类继承重写实现原理解析
Apr 03 Python
Jupyter Notebook折叠输出的内容实例
Apr 22 Python
Pytorch环境搭建与基本语法
Jun 03 Python
python3 循环读取excel文件并写入json操作
Jul 14 Python
python实现三壶谜题的示例详解
Nov 02 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
ThinkPHP 连接Oracle数据库的详细教程[全]
2012/07/16 PHP
PHP基于单例模式实现的数据库操作基类
2016/01/15 PHP
Linux服务器下PHPMailer发送邮件失败的问题解决
2017/03/04 PHP
js 变量类型转换常用函数与代码[比较全]
2009/12/01 Javascript
基于jquery的jqDnR拖拽溢出的修改
2011/02/12 Javascript
JavaScript对象数组排序函数及六个用法
2015/12/23 Javascript
JavaScript数据结构与算法之链表
2016/01/29 Javascript
JavaScript数据类型转换的注意事项
2016/07/31 Javascript
微信小程序 require机制详解及实例代码
2016/12/14 Javascript
快速实现jQuery多级菜单效果
2017/02/01 Javascript
JavaScript通过filereader接口读取文件
2017/05/10 Javascript
JS开发中基本数据类型具体有哪几种
2017/10/19 Javascript
JS返回顶部实例代码
2020/08/09 Javascript
JavaScript解决浮点数计算不准确问题的方法分析
2018/07/09 Javascript
用vuex写了一个购物车H5页面的示例代码
2018/12/04 Javascript
Javascript读写cookie的实例源码
2019/03/16 Javascript
JQuery复选框全选效果如何实现
2020/05/08 jQuery
[02:16]卖萌的僵尸 DOTA2神话信使飞僵小宝来袭
2014/03/24 DOTA
[20:46]Ti4循环赛第三日VG vs DK
2014/07/12 DOTA
python轻松实现代码编码格式转换
2015/03/26 Python
Python 自动化表单提交实例代码
2017/06/08 Python
python自动发送邮件脚本
2018/06/20 Python
Python学习笔记之列表推导式实例分析
2019/08/13 Python
关于pytorch多GPU训练实例与性能对比分析
2019/08/19 Python
Python实现生成密码字典的方法示例
2019/09/02 Python
python Django 反向访问器的外键冲突解决
2020/05/20 Python
keras分类模型中的输入数据与标签的维度实例
2020/07/03 Python
Python如何绘制日历图和热力图
2020/08/07 Python
英国Amara家居法国网站:家居装饰,现代装饰和豪华礼品
2016/12/15 全球购物
新浪微博实习心得体会
2014/01/27 职场文书
建筑公司员工自我鉴定
2014/04/08 职场文书
环保倡议书50字
2014/05/15 职场文书
思想道德自我评价2015
2015/03/09 职场文书
证婚人婚礼致辞
2015/07/28 职场文书
2015年第31个教师节致辞
2015/07/31 职场文书
Java 中的 Unsafe 魔法类的作用大全
2021/06/26 Java/Android