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 经典数字滤波实例

数字滤波分为 IIR 滤波,和FIR 滤波。 FIR 滤波: import scipy.signal as signal import numpy as np import pyla...

python操作mongodb根据_id查询数据的实现方法

本文实例讲述了python操作mongodb根据_id查询数据的实现方法。分享给大家供大家参考。具体分析如下: _id是mongodb自动生成的id,其类型为ObjectId,所以如果需...

PyQt5根据控件Id获取控件对象的方法

如下所示: self.findChild(QComboBox, "name") self is class first parameter is Type second pa...

opencv python 2D直方图的示例代码

opencv python 2D直方图的示例代码

Histograms - 3 : 2D Histograms 我们已经计算并绘制了一维直方图,因为我们只考虑一个特征,即像素的灰度强度值.但在二维直方图中,需要考虑两个特征,通常,它用...

解析Python中的变量、引用、拷贝和作用域的问题

解析Python中的变量、引用、拷贝和作用域的问题

在Python中,变量是没有类型的,这和以往看到的大部分编辑语言都不一样。在使用变量的时候,不需要提前声明,只需要给这个变量赋值即可。但是,当用变量的时候,必须要给这个变量赋值;如果只写...