资源简介
一个简单实用的Python3源代码,供围棋截屏图片扫描成通用的sgf格式
To use the script: python go-ocr.py images
To use the script: python go-ocr.py images

代码片段和文件信息
#!/usr/bin/env python
from PIL import Image
import os
import string
import sys
image_dir = sys.argv[1]
WHITE = (255 255 255)
BLACK = (0 0 0)
BORDER = 9
INTERVAL = 19
# use a small offset from intersection to make sure we don‘t grab a
# black pixel from the grid line as opposed to a black pixel from a stone
OFFSET = 3
# TL = Top Left TR = Top Right
# BL = Bottom Left BR = Bottom Right
# the corner you want the image flipped from
ORIGIN_CORNER = ‘TR‘
# the corner you want the image flipped to
TARGET_CORNER = ‘TL‘
def transpose_image(image x_lines y_lines):
transpose_matrix = {
(‘TL‘ ‘TR‘): ‘V‘
(‘TL‘ ‘BL‘): ‘H‘
(‘TL‘ ‘BR‘): ‘HV‘
(‘TR‘ ‘TL‘): ‘V‘
(‘TR‘ ‘BL‘): ‘HV‘
(‘TR‘ ‘BR‘): ‘H‘
(‘BL‘ ‘TL‘): ‘H‘
(‘BL‘ ‘TR‘): ‘HV‘
(‘BL‘ ‘BR‘): ‘V‘
(‘BR‘ ‘TL‘): ‘HV‘
(‘BR‘ ‘TR‘): ‘H‘
(‘BR‘ ‘BL‘): ‘V‘}
if TARGET_CORNER == ‘TL‘:
x_string = y_string = string.ascii_letters
elif TARGET_CORNER == ‘TR‘:
x_string = string.ascii_letters[19 - x_lines:19]
y_string = string.ascii_letters
elif TARGET_CORNER == ‘BL‘:
x_string = string.ascii_letters
y_string = string.ascii_letters[19 - y_lines:19]
elif TARGET_CORNER == ‘BR‘:
x_string = string.ascii_letters[19 - x_lines:19]
y_string = string.ascii_letters[19 - y_lines:19]
for instruction in transpose_matrix[(ORIGIN_CORNER TARGET_CORNER)]:
if instruction == ‘H‘:
image = image.transpose(Image.FLIP_TOP_BOTTOM)
if instruction == ‘V‘:
image = image.transpose(Image.FLIP_LEFT_RIGHT)
return image x_string y_string
for root dir files in os.walk(image_dir):
for image in files:
path = os.path.join(root image)
try:
im = Image.open(path)
except:
print((“Cannot open ‘{}‘. Not supported or not an image file“.format(path)))
continue
width height = im.size
#x_lines = width / INTERVAL
#y_lines = height / INTERVAL
x_lines = int(width / INTERVAL)
y_lines = int(height / INTERVAL)
im x_string y_string = transpose_image(im x_lines y_lines)
# deduct 1 from total lines because first line starts on offset
x_lines = x_lines - 1
y_lines = y_lines - 1
rgbimg = im.convert(‘RGB‘)
pixels = rgbimg.load()
coords = {
‘AB‘: []
‘AW‘: []}
index_x_string = 0
for x in range(BORDER (INTERVAL * x_lines) + INTERVAL INTERVAL):
index_y_string = 0
for y in range(BORDER (INTERVAL * y_lines) + INTERVAL INTERVAL):
# get pixel using no offset:
# if it‘s white then it‘s a white stone
# if it‘s black:
# get pixel using offset:
# if it‘s black then it‘s a black stone
#
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2020-01-07 11:12 go-ocr\
文件 3801 2020-01-06 16:45 go-ocr\go-ocr.py
目录 0 2020-01-07 10:58 go-ocr\images\
文件 485 2013-04-03 12:41 go-ocr\images\image_1.png
文件 99 2020-01-06 22:51 go-ocr\images\image_1.sgf
文件 663 2013-04-03 12:41 go-ocr\images\image_2.png
文件 151 2020-01-06 22:51 go-ocr\images\image_2.sgf
文件 171 2020-01-07 11:12 go-ocr\README.md
相关资源
- 二级考试python试题12套(包括选择题和
- pywin32_python3.6_64位
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
- 基于PyQt5的视频播放器设计
- 一个简单的python爬虫
- csv文件行列转换python实现代码
- Python操作Mysql教程手册
- Python Machine Learning Case Studies
- python获取硬件信息
评论
共有 条评论