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 re正则表达式模块(Regular Expression)
Jul 16 Python
Python中使用PIL库实现图片高斯模糊实例
Feb 08 Python
python实现中文输出的两种方法
May 09 Python
python中threading超线程用法实例分析
May 16 Python
Python黑帽编程 3.4 跨越VLAN详解
Sep 28 Python
python爬虫获取京东手机图片的图文教程
Dec 29 Python
django的登录注册系统的示例代码
May 14 Python
Python 中 function(#) (X)格式 和 (#)在Python3.*中的注意事项
Nov 30 Python
python模拟键盘输入 切换键盘布局过程解析
Aug 15 Python
Python3+RIDE+RobotFramework自动化测试框架搭建过程详解
Sep 23 Python
简述python&pytorch 随机种子的实现
Oct 07 Python
python tqdm实现进度条的示例代码
Nov 10 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
phpize的深入理解
2013/06/03 PHP
Yii2分页的使用及其扩展方法详解
2016/05/23 PHP
YII2自动登录Cookie总是失效的解决方法
2017/06/28 PHP
Laravel框架实现的上传图片到七牛功能详解
2019/09/06 PHP
javascript document.images实例
2008/05/27 Javascript
js获取网页高度(详细整理)
2012/12/28 Javascript
js函数内变量的作用域分析
2015/01/12 Javascript
RequireJS使用注意细节
2016/05/15 Javascript
Node.js中常规的文件操作总结
2016/10/13 Javascript
利用prop-types第三方库对组件的props中的变量进行类型检测
2017/05/02 Javascript
jQuery中的deferred对象和extend方法详解
2017/05/08 jQuery
jquery 获取索引值在一定范围的列表方法
2018/01/25 jQuery
Node.js 利用cheerio制作简单的网页爬虫示例
2018/03/01 Javascript
vue webpack实用技巧总结
2018/04/24 Javascript
[02:55]DOTA2英雄基础教程 发条技师
2013/12/04 DOTA
[00:35]可解锁地面特效
2018/12/20 DOTA
Python的净值数据接口调用示例分享
2016/03/15 Python
python中 logging的使用详解
2017/10/25 Python
Django如何开发简单的查询接口详解
2019/05/17 Python
django admin.py 外键,反向查询的实例
2019/07/26 Python
Python学习笔记之文件的读写操作实例分析
2019/08/07 Python
python shutil文件操作工具使用实例分析
2019/12/25 Python
深入了解NumPy 高级索引
2020/07/24 Python
python 实现单例模式的5种方法
2020/09/23 Python
使用CSS3制作饼状旋转载入效果的实例
2015/06/23 HTML / CSS
天巡全球:Skyscanner Global
2017/06/20 全球购物
俄罗斯花园种植材料批发和零售网上商店:Беккер
2019/07/22 全球购物
分别介绍一下Session Bean和Entity Bean
2015/03/13 面试题
食堂个人先进事迹
2014/01/22 职场文书
机械专业应届毕业生自荐书
2014/06/12 职场文书
建筑结构施工求职信
2014/07/11 职场文书
工作保证书怎么写
2015/02/28 职场文书
mysql批量新增和存储的方法实例
2021/04/07 MySQL
基于python制作简易版学生信息管理系统
2021/04/20 Python
MySQL中一条update语句是如何执行的
2022/03/16 MySQL
Python安装使用Scrapy框架
2022/04/12 Python