1. 程式人生 > >macOS python3 opencv PIL.Image 二值化影象

macOS python3 opencv PIL.Image 二值化影象

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()
  • 原圖
    在這裡插入圖片描述
  • 二值化圖
    在這裡插入圖片描述

參考:

  1. python驗證碼識別1:灰度處理、二值化、降噪、tesserocr識別
  2. Pillow (PIL Fork) Image Module