簡體   English   中英

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

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

在這樣的游戲板中,我想檢測所有數字以及這些數字的位置。 關於如何做的任何想法?

Python 中是否存在任何庫,我可以導入這些庫來識別這些數字,以及它們在這樣的游戲板上的位置?

游戲板在此處輸入圖像描述

此處缺少一些結果,但您可以從以下方法開始:

  1. 將圖像裁剪為“細胞”
  2. tesseract識別每個“細胞”:

代碼:

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:

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