Python Pillow Image Invert

yipeiwu_com5年前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实现多线程网页下载器

本文为大家分享了python实现的一个多线程网页下载器,供大家参考,具体内容如下 这是一个有着真实需求的实现,我的用途是拿它来通过 HTTP 方式向服务器提交游戏数据。把它放上来也是想大...

CentOS 7下Python 2.7升级至Python3.6.1的实战教程

CentOS 7下Python 2.7升级至Python3.6.1的实战教程

前言 大家应该都知道,Centos是目前最为流行的Linux服务器系统,其默认的Python 2.x,但是根据python社区的规划,在不久之后,整个社区将向Python3迁移,且将不在...

对Tensorflow中权值和feature map的可视化详解

对Tensorflow中权值和feature map的可视化详解

前言 Tensorflow中可以使用tensorboard这个强大的工具对计算图、loss、网络参数等进行可视化。本文并不涉及对tensorboard使用的介绍,而是旨在说明如何通过代...

Python简直是万能的,这5大主要用途你一定要知道!(推荐)

从2015开始国内就开始慢慢接触Python了,从16年开始Python就已经在国内的热度更高了,目前也可以算的上"全民Python"了。 众所周知小学生的教材里面已经有Python了,...

Python3中的列表,元组,字典,字符串相关知识小结

一、知识概要   1. 列表,元组,字典,字符串的创建方式   2. 列表,元组,字典,字符串的方法调用   3. 列表,元组,字典,字符串的常规用法 二、列表 # 列 表 # 列...