使用PIL(Python-Imaging)反转图像的颜色方法


Posted in Python onJanuary 24, 2019

利用PIL将图片转换为黑色与白色反转的图片,下面笔者小白介绍如何实现。

解决方案一:

from PIL import Image
import PIL.ImageOps  
#读入图片
image = Image.open('your_image.png')
#反转
inverted_image = PIL.ImageOps.invert(image)
#保存图片
inverted_image.save('new_name.png')

注意:“ImageOps模块包含多个'ready-made'图像处理操作,该模块有些实验性,大多数操作符只适用于L和RGB图像。”

解决方案二:

如果图像是RGBA透明的,参考如下代码。

from PIL import Image
import PIL.ImageOps  

image = Image.open('your_image.png')
if image.mode == 'RGBA':
  r,g,b,a = image.split()
  rgb_image = Image.merge('RGB', (r,g,b))

  inverted_image = PIL.ImageOps.invert(rgb_image)

  r2,g2,b2 = inverted_image.split()

  final_transparent_image = Image.merge('RGBA', (r2,g2,b2,a))

  final_transparent_image.save('new_file.png')

else:
  inverted_image = PIL.ImageOps.invert(image)
  inverted_image.save('new_name.png')

解决方案三:

注:对于使用”1″模式的图像(即,1位像素,黑白色,以每个字节为单位存储的see docs),您需要在调用PIL.ImageOps.invert之前将其转换为”L”模式。

im = im.convert('L')
im = ImageOps.invert(im)
im = im.convert('1')

以上这篇使用PIL(Python-Imaging)反转图像的颜色方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中endswith()函数的基本使用
Apr 07 Python
Python实现批量检测HTTP服务的状态
Oct 27 Python
Python 遍历列表里面序号和值的方法(三种)
Feb 17 Python
在Python web中实现验证码图片代码分享
Nov 09 Python
Centos7 Python3下安装scrapy的详细步骤
Mar 15 Python
Python 实现使用dict 创建二维数据、DataFrame
Apr 13 Python
[原创]Python入门教程1. 基本运算【四则运算、变量、math模块等】
Oct 28 Python
分享Python切分字符串的一个不错方法
Dec 14 Python
python 字典的打印实现
Sep 26 Python
TensorFlow索引与切片的实现方法
Nov 20 Python
解决python web项目意外关闭,但占用端口的问题
Dec 17 Python
详解用Python爬虫获取百度企业信用中企业基本信息
Jul 02 Python
Python3实现取图片中特定的像素替换指定的颜色示例
Jan 24 #Python
python 实现图片旋转 上下左右 180度旋转的示例
Jan 24 #Python
Python对象与引用的介绍
Jan 24 #Python
selenium+python自动化测试之多窗口切换
Jan 23 #Python
python 去除二维数组/二维列表中的重复行方法
Jan 23 #Python
selenium+python自动化测试之鼠标和键盘事件
Jan 23 #Python
selenium+python自动化测试之页面元素定位
Jan 23 #Python
You might like
代码精简的可以实现元素圆角的js函数
2007/07/21 Javascript
js 动态选中下拉框
2009/11/26 Javascript
div模拟滚动条效果示例代码
2013/10/16 Javascript
基于JQuery实现的Select级联
2014/01/27 Javascript
动态加载js、css等文件跨iframe实现
2014/02/24 Javascript
js调用iframe实现打印页面内容的方法
2014/03/04 Javascript
Underscore.js 1.3.3 中文注释翻译说明
2015/06/25 Javascript
js改变style样式和css样式的简单实例
2016/06/28 Javascript
webpack入门必知必会
2017/01/16 Javascript
JavaScript中的一些隐式转换和总结(推荐)
2017/12/22 Javascript
JavaScript使用math.js进行精确计算操作示例
2018/06/19 Javascript
Element InputNumber 计数器的实现示例
2020/08/03 Javascript
vue中axios封装使用的完整教程
2021/03/03 Vue.js
Python ORM框架SQLAlchemy学习笔记之映射类使用实例和Session会话介绍
2014/06/10 Python
在Python中编写数据库模块的教程
2015/04/29 Python
python引入导入自定义模块和外部文件的实例
2017/07/24 Python
Django读取Mysql数据并显示在前端的实例
2018/05/27 Python
python3 打开外部程序及关闭的示例
2018/11/06 Python
python 标准差计算的实现(std)
2019/07/29 Python
Django实现分页显示效果
2019/10/31 Python
python cv2在验证码识别中应用实例解析
2019/12/25 Python
python3爬取torrent种子链接实例
2020/01/16 Python
tensorflow获取预训练模型某层参数并赋值到当前网络指定层方式
2020/01/24 Python
Python多线程Threading、子线程与守护线程实例详解
2020/03/24 Python
详解用selenium来下载小姐姐图片并保存
2021/01/26 Python
美国知名的时尚购物网站:Anthropologie
2016/12/22 全球购物
Hunkemöller瑞士网上商店:欧洲最大的内衣品牌之一
2018/12/03 全球购物
SQL中where和having的区别
2012/06/17 面试题
园长自我鉴定
2013/10/06 职场文书
中专毕业自我鉴定
2013/10/16 职场文书
策划助理岗位职责
2013/11/18 职场文书
大学生创业项目方案
2014/03/08 职场文书
市政管理求职信范文
2014/05/07 职场文书
承诺书范文
2014/06/03 职场文书
学习焦裕禄观后感
2015/06/09 职场文书
MySQL安装后默认自带数据库的作用详解
2021/04/27 MySQL