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对验证码降噪的实现示例代码

python对验证码降噪的实现示例代码

前言: 最近写爬虫会经常遇到一些验证码识别的问题,现如今的验证码已经是五花八门,刚开始的验证码就是简单的对生成的验证码图片进行一些干扰,但是随着计算机视觉库的 发展壮大,可以轻松解决简单...

详解Python中的动态属性和特性

导语:本文章记录了本人在学习Python基础之元编程篇的重点知识及个人心得,打算入门Python的朋友们可以来一起学习并交流。 一、利用动态属性处理JSON数据源 属性:在Python...

python实现k均值算法示例(k均值聚类算法)

简单实现平面的点K均值分析,使用欧几里得距离,并用pylab展示。 复制代码 代码如下:import pylab as pl #calc Euclid squiredef calc_e_...

Python信息抽取之乱码解决办法

Python信息抽取之乱码解决办法 就事论事,直说自己遇到的情况,和我不一样的路过吧,一样的就看看吧   信息抓取,用python,beautifulSoup,lxml,re,urlli...

python numpy 矩阵堆叠实例

在实际操作中,遇到了矩阵堆叠的操作,本来想着自己写一个函数,后来想,应该有库函数,于是一阵找寻 import numpy as np a = np.array([1,2,3]) b...