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 调用VC++的动态链接库(DLL)

1. 首先VC++的DLL的导出函数定义成标准C的导出函数: 复制代码 代码如下:#ifdef LRDLLTEST_EXPORTS #define LRDLLTEST_API __dec...

Python 过滤字符串的技巧,map与itertools.imap

具体的实例 我们需要在目录中遍历,包括子目录(哈哈),找出所有后缀为:rmvb ,avi ,pmp 的文件。(天哪?!你要干什么?这可是我的隐私啊~~) 复制代码 代码如下:import...

在Python3中初学者应会的一些基本的提升效率的小技巧

有时候我反问我自己,怎么不知道在Python 3中用更简单的方式做“这样”的事,当我寻求答案时,随着时间的推移,我当然发现更简洁、有效并且bug更少的代码。总的来说(不仅仅是这篇文章),...

pandas中的DataFrame按指定顺序输出所有列的方法

问题: 输出新建的DataFrame对象时,DataFrame中各列的显示顺序和DataFrame定义中的顺序不一致。 例如: import pandas as pd grades...

用python 实现在不确定行数情况下多行输入方法

如下所示: stopword = '' str = '' for line in iter(raw_input, stopword): str += line + '\n' pri...