资源简介
一个简单实用的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实现算术编码
- [python] Kmeans文本聚类算法+PAC降维+Ma
- mysqldb64位
- abaqus激光增材仿真,生死单元添加p
- 利用Python爬虫批量百度图库图片
- 基于python的深度信念网络
- win10下调用OpenCV-Python和YOLACT模型进行
- Python多线程子域名扫描自带字典
- modbus通信的Python实现
- python批量pdf转txt
- 遗传算法python代码
- 爬取京东评论。代码
- 迷宫问题的A*算法(python实现)
- Mod_Python2.7安装文件
- 王硕-你也能看懂的python算法书-随书代
- 使用Python实现的网络社团发现GN算法
- python3的ARP简单攻击脚本
- 详解python实现FP-TREE进行关联规则挖掘
- Python 正则表达式操作指南 Regular ex<
- k匿名隐私保护算法python版
- Python人工智能AI深度学习全套课程.t
- python实现的使用huffman编码对文本的压
- 爬取58同城
- python提取点云数据
- 千锋python爬虫教程之scrapy框架.txt
- Python教学视频哪个好
- 小甲鱼python课程96集包含源码+课件+课
- 小甲鱼python课程96集含源码课件课后习
- Python从入门到精通教程共40G.txt
- python与json
评论
共有 条评论