简体   繁体   English

如何用 Python 找出图像中的所有数字及其位置?

[英]How to find out all the numbers and the positions of them in an image with Python?

In such a game board, I want to detect all the numbers, together with the positions of these numbers.在这样的游戏板中,我想检测所有数字以及这些数字的位置。 Any ideas about how to do it?关于如何做的任何想法?

Do there exist any libraries in Python which I can import to recognize these numbers, together with the positions of them on such a game board? Python 中是否存在任何库,我可以导入这些库来识别这些数字,以及它们在这样的游戏板上的位置?

Game Board游戏板在此处输入图像描述

Some results are missing here but you can start with following approach:此处缺少一些结果,但您可以从以下方法开始:

  1. Crop image to "cells"将图像裁剪为“细胞”
  2. Recognize each "cell" with tesseract :tesseract识别每个“细胞”:

Code:代码:

import pytesseract
from PIL import Image

pytesseract.pytesseract.tesseract_cmd = r'c:\Program Files\Tesseract-OCR\tesseract.exe'

img = Image.open('image.png')
for x, ltr in enumerate('abcdefghijklmno'):
    for y, nbr in enumerate(range(15, 0, -1)):
        x_start = 20+x*30
        y_start = 20+y*30
        img_crop = img.crop((x_start, y_start, x_start+20, y_start+20))
        res = pytesseract.image_to_string(img_crop).replace('\f', '').replace('\n', '')
        if res:
            print(f'{ltr}{nbr} - {res}')

Output: Output:

b9 - 39
c7 - 44
c6 - 22
d9 - 37
f11 - 31
f10 - 42
f4 - 24
f3 - 23
g11 - 20
h11 - 26
i12 - 25
j11 - 27
j9 - 21
k10 - 28

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM