SoFunction
Updated on 2024-11-21

Python image binarization to improve recognition rate code example

This article introduces the python image binarization to improve the recognition rate of code examples, the text through the sample code is very detailed, for everyone's learning or work has a certain reference learning value, you can refer to the next!

The code is as follows

import cv2from PIL import Imagefrom pytesseract import pytesseractfrom PIL import ImageEnhanceimport reimport string
def createFile(filePath,newFilePath):

  img = (filePath)

  # Mode L" is a gray image, each pixel of which is represented by 8 bits, 0 for black, 255 for white, and other numbers for different shades of gray.
  Img = ('L')
  (newFilePath)

  # Custom grayscale boundaries, greater than this value is black, less than this value is white
  threshold = 200

  table = []
  for i in range(256):
    if i < threshold:
      (0)
    else:
      (1)

  # Image binarization
  photo = (table, '1')
  (newFilePath)
if __name__ == '__main__':
createFile(r'',r'')

Original image:

Processed Figures:

Identify the results:

This is the entire content of this article.