X-Git-Url: http://git.tomasm.cz/imago.git/blobdiff_plain/3fa492032fb6f72fc8d153c0d11ad61155e8c1b9..c13a5fb97c22ae952b4a99e75735f9c96efcf438:/intrsc.py?ds=inline diff --git a/intrsc.py b/intrsc.py index 564faaa..ba36b6d 100644 --- a/intrsc.py +++ b/intrsc.py @@ -1,41 +1,67 @@ -from math import sin, cos, tan +"""Imago intersections module""" + +from math import cos, tan, pi from operator import itemgetter -import Image, ImageDraw +import ImageDraw + +def dst(line): + """Return normalized line.""" + if line[0] < pi / 2: + line = line[0] + pi, - line[1] + return line + +def dst_sort(lines): + """Return lines sorted by distance.""" + l_max = max(l[0] for l in lines) + l_min = min(l[0] for l in lines) + if l_max - l_min > (3. / 4) * pi: + lines = [dst(l) for l in lines] + lines.sort(key=itemgetter(1)) + return lines def board(image, lines, show_all, do_something): + """Compute intersections, find stone colors and return board situation.""" + lines = [dst_sort(l) for l in lines] intersections = intersections_from_angl_dist(lines, image.size) - image_g = image.copy() - draw = ImageDraw.Draw(image_g) - for line in intersections: - for (x, y) in line: - draw.point((x , y), fill=(120, 255, 120)) if show_all: + image_g = image.copy() + draw = ImageDraw.Draw(image_g) + for line in intersections: + for (x, y) in line: + draw.point((x , y), fill=(120, 255, 120)) do_something(image_g, "intersections") - board = [] + board_r = [] + board_raw = [] for line in intersections: - board.append([stone_color(image, intersection) for intersection in + board_r.append([stone_color(image, intersection) for intersection in + line]) + board_raw.append([stone_color_raw(image, intersection) for intersection in line]) - return board + return board_r, board_raw -def intersections_from_angl_dist(lines, size): +def intersections_from_angl_dist(lines, size, get_all=True): + """Take grid-lines and size of the image. Return intersections.""" intersections = [] - for (angl1, dist1) in sorted(lines[1], key=itemgetter(1)): + for (angl1, dist1) in lines[1]: line = [] - for (angl2, dist2) in sorted(lines[0], key=itemgetter(1)): + for (angl2, dist2) in lines[0]: if abs(angl1 - angl2) > 0.4: - x = - ((dist2 / cos(angl2)) - (dist1 / cos(angl1))) / (tan(angl1) - tan(angl2)) - y = (tan(angl1) * x) - (dist1 / cos(angl1)) - if (-size[0] / 2 < x < size[0] / 2 and - -size[1] / 2 < y < size[1] / 2): - line.append((int(x + size[0] / 2), int(y + size[1] / 2))) + i_x = (- ((dist2 / cos(angl2)) - (dist1 / cos(angl1))) + / (tan(angl1) - tan(angl2))) + i_y = (tan(angl1) * i_x) - (dist1 / cos(angl1)) + if get_all or (-size[0] / 2 < i_x < size[0] / 2 and + -size[1] / 2 < i_y < size[1] / 2): + line.append((int(i_x + size[0] / 2), + int(i_y + size[1] / 2))) intersections.append(line) return intersections def stone_color(image, (x, y)): + """Given image and coordinates, return stone color.""" suma = 0. for i in range(-2, 3): for j in range(-2, 3): @@ -50,3 +76,16 @@ def stone_color(image, (x, y)): return '.' else: return 'W' + +def stone_color_raw(image, (x, y)): + """Given image and coordinates, return stone color.""" + suma = [] + for i in range(-2, 3): + for j in range(-2, 3): + try: + suma.append(image.getpixel((x + i, y + j))) + except IndexError: + pass + suma = (sum(s[0] for s in suma) / 25., sum(s[1] for s in suma) / 25., + sum(s[2] for s in suma) / 25.) + return suma