Python Pillow Image Invert

yipeiwu_com6年前Python基础

本文主要是利用Python的第三方库Pillow,实现单通道灰度图像的颜色翻转功能。

# -*- encoding:utf-8 -*-
import os
import sys
from PIL import Image
from PIL import ImageOps
def img_gray_invert(img_path):
  """
  invert input image.
  """
  if not os.path.isfile(img_path):
    print "Error for input file path."
    return
  image = Image.open(img_path)
  image = image.convert("L")
  inverted_image = ImageOps.invert(image)
  return inverted_image
if __name__ == '__main__':
  argv = sys.argv
  if len(argv) != 3:
    print """Example:
    python gray_invert.py test/htc.png test/htc_inv.png
    """
  else:
    img_file_path = argv[1]
    invert_image = img_gray_invert(img_file_path)
    img_file_out = argv[2]
    invert_image.save(img_file_out)

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对【听图阁-专注于Python设计】的支持。如果你想了解更多相关内容请查看下面相关链接

相关文章

用PyInstaller把Python代码打包成单个独立的exe可执行文件

用PyInstaller把Python代码打包成单个独立的exe可执行文件

之前就想要把自己的BlogsToWordpress打开成exe了。一直没去弄。 又看到有人提到python打开成exe的问题。 所以打算现在就去试试。 注:此处之所有选用BlogsToW...

Python 数据结构之旋转链表

题目描述:给定一个链表,旋转链表,使得每个节点向右移动k个位置,其中k是一个非负数 样例:给出链表1->2->3->4->5->null和k=2;返回4-...

Python SqlAlchemy动态添加数据表字段实例解析

Python SqlAlchemy动态添加数据表字段实例解析

本文研究的主要是Python SqlAlchemy动态添加数据表字段,具体如下。 我们知道使用SqlAlchemy创建类来映射数据表,类属性等于数据库字段,但有时候要在我们创建表的时候,...

python中协程实现TCP连接的实例分析

在网络通信中,每个连接都必须创建新线程(或进程) 来处理,否则,单线程在处理连接的过程中, 无法接受其他客户端的连接。所以我们尝试使用协程来实现服务器对多个客户端的响应。 与单一TCP...

Python使用Tkinter实现转盘抽奖器的步骤详解

Python使用Tkinter实现转盘抽奖器的步骤详解

我使用 Python 中的 Tkinter 模块实现了一个简单的滚动抽奖器,接下来继续写一个简单的转盘抽奖器。 Tkinter 实现滚动抽奖器参考:/post/177913.htm 滚动...