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设计】的支持。如果你想了解更多相关内容请查看下面相关链接

相关文章

Python、 Pycharm、Django安装详细教程(图文)

Python、 Pycharm、Django安装详细教程(图文)

最近做项目要用到python,那么不用说就得先配置好python环境 以及选择好python工具。接下来分享自己的安装过程。 (一)、Python的安装 1.先进入官网下载python版...

Django基础之Model操作步骤(介绍)

Django基础之Model操作步骤(介绍)

一、数据库操作 1、创建model表 基本结构: #coding:Utf8 from django.db import models class userinfo(models....

Windows平台Python编程必会模块之pywin32介绍

Windows平台Python编程必会模块之pywin32介绍

在Windows平台上,从原来使用C/C++编写原生EXE程序,到使用Python编写一些常用脚本程序,成熟的模块的使用使得编程效率大大提高了。 不过,python模块虽多,也不可能满足...

Python实现Mysql数据统计及numpy统计函数

Python实现Mysql数据统计的实例代码如下所示: import pymysql import xlwt excel=xlwt.Workbook(encoding='utf-8'...

python enumerate函数的使用方法总结

enumerate函数用于遍历序列中的元素以及它们的下标。 enumerate函数说明: enumerate()是python的内置函数 enumerate在字典上是枚举、列举的意思...