python实现图片二值化及灰度处理方式

yipeiwu_com6年前Python基础

我就废话不多说了,直接上代码吧!

集成环境:win10 pycharm


#!/usr/bin/env python3.5.2
# -*- coding: utf-8 -*-
'''4图片灰度调整及二值化:
集成环境:win10 python3 Pycharm
'''

from PIL import Image

# load a color image
im = Image.open('picture\\haha.png' )#当前目录创建picture文件夹

# convert to grey level image
Lim = im.convert('L' )
Lim.save('pice.jpg' )

# setup a converting table with constant threshold
threshold = 185
table = []
for i in range(256):
  if i < threshold:
    table.append(0)
  else:
    table.append(1)

# convert to binary image by the table
bim = Lim.point(table, '1' )

bim.save('picf.png' )

以上这篇python实现图片二值化及灰度处理方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python 登录网站详解及实例

Python 登录网站详解及实例 对于大部分论坛,我们想要抓取其中的帖子分析,首先需要登录,否则无法查看。   这是因为 HTTP 协议是一个无状态(Stateless)的协议,服务器如...

浅析Python3中的对象垃圾收集机制

###概述 GC作为现代编程语言的自动内存管理机制,专注于两件事:1. 找到内存中无用的垃圾资源 2. 清除这些垃圾并把内存让出来给其他对象使用。 在Python中,它在每个对象中保持了...

用python画一只可爱的皮卡丘实例

用python画一只可爱的皮卡丘实例

效果图 #!/usr/bin/env python # -*- coding:utf-8 -*- from turtle import * ''' 绘制皮卡丘头部 ''' de...

python进阶教程之函数对象(函数也是对象)

秉承着一切皆对象的理念,我们再次回头来看函数(function)。函数也是一个对象,具有属性(可以使用dir()查询)。作为对象,它还可以赋值给其它对象名,或者作为参数传递。 lambd...

Python实现读取SQLServer数据并插入到MongoDB数据库的方法示例

本文实例讲述了Python实现读取SQLServer数据并插入到MongoDB数据库的方法。分享给大家供大家参考,具体如下: # -*- coding: utf-8 -*- impo...