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 Django里CSRF 对应策略详解

CSRF(Cross Site Request Forgery, 跨站域请求伪造)是一种网络的×××方式。 我的理解是,比如你访问过招商银行的网站并登陆之后,你的cookie信息暂时不会...

Python的iOS自动化打包实例代码

Python的iOS自动化打包实例代码

前言 这段时间刚刚学习了一段时间的Python,加上自己是做iOS开发的,就想着用Python来做一个自动化打包,可以自动完成打包,上传到蒲公英,并且发送邮箱给测试人员. 一是可以减...

Python 绘制酷炫的三维图步骤详解

Python 绘制酷炫的三维图步骤详解

通常我们用 Python 绘制的都是二维平面图,但有时也需要绘制三维场景图,比如像下面这样的: 这些图怎么做出来呢?今天就来分享下如何一步步绘制出三维矢量(SVG)图。 八面体 我们先...

pytorch 指定gpu训练与多gpu并行训练示例

一. 指定一个gpu训练的两种方法: 1.代码中指定 import torch torch.cuda.set_device(id) 2.终端中指定 CUDA_VISIBLE_DEV...

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

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

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