macOS python3 opencv PIL.Image 二值化影象
阿新 • • 發佈:2018-11-13
Image 影象 二值化
1,遍歷所有畫素
0 黑色
255 白色
設定閥值為127,大於閥值的白色
#! /usr/local/bin/python3
# coding:utf-8
from PIL import Image
import pytesseract
"""
p1 = Image.open("/root/8069.jpg")
text = pytesseract.image_to_string(p1)
print (text)
"""
img = Image.open("/root/8096002.jpg")
#img.show()
# 影象轉換為灰度
img = img.convert("L")
img.show()
#load() 讀取畫素
pixdata = img.load()
w ,h = img.size
for y in range(h):
for x in range(w):
if pixdata[x,y] < 127:
pixdata[x,y] = 0
else:
pixdata[x,y] = 255
img.show()
- 原圖
- 二值化圖
參考: