From 3fa492032fb6f72fc8d153c0d11ad61155e8c1b9 Mon Sep 17 00:00:00 2001 From: Tomas Musil Date: Fri, 12 Oct 2012 00:04:35 +0200 Subject: [PATCH] moving code around --- imago.py | 55 +++++-------------------------------------------------- intrsc.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ linef.py | 6 ++++++ manual.py | 2 +- 4 files changed, 64 insertions(+), 51 deletions(-) create mode 100644 intrsc.py diff --git a/imago.py b/imago.py index decc7a6..a0eabfd 100755 --- a/imago.py +++ b/imago.py @@ -6,7 +6,6 @@ import sys import os import math import argparse -from operator import itemgetter try: import Image, ImageDraw @@ -17,6 +16,7 @@ except ImportError, msg: import im_debug import linef import manual +import intrsc def main(): """Main function of the program.""" @@ -64,45 +64,14 @@ def main(): else: lines = linef.find_lines(image, show_all, do_something, verbose) - image_g = image.copy() - draw = ImageDraw.Draw(image_g) - for line in [l for s in lines for l in s]: - draw.line(linef.line_from_angl_dist(line, image.size), fill=(120, 255, 120)) - if show_all: - do_something(image_g, "the grid") - - 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)) - - for line in intersections: - print ' '.join([stone_color(image, intersection) for intersection in - line]) + board = intrsc.board(image, lines, show_all, do_something) - if show_all: - do_something(image_g, "intersections") + #simple ASCII output: + for line in board: + print ' '.join(line) return 0 -def stone_color(image, (x, y)): - suma = 0. - for i in range(-2, 3): - for j in range(-2, 3): - try: - suma += sum(image.getpixel((x + i, y + j))) - except IndexError: - pass - suma /= 3 * 25 - if suma < 55: - return 'B' - elif suma < 200: - return '.' - else: - return 'W' - class imsave(): def __init__(self, saving_dir): self.saving_dir = saving_dir @@ -115,20 +84,6 @@ class imsave(): image.save(filename, 'JPEG') self.saving_num += 1 -def intersections_from_angl_dist(lines, size): - intersections = [] - for (angl1, dist1) in sorted(lines[1], key=itemgetter(1)): - line = [] - for (angl2, dist2) in sorted(lines[0], key=itemgetter(1)): - if abs(angl1 - angl2) > 0.4: - x = - ((dist2 / math.cos(angl2))-(dist1 / math.cos(angl1))) / (math.tan(angl1) - math.tan(angl2)) - y = (math.tan(angl1) * x) - (dist1 / math.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))) - intersections.append(line) - return intersections - if __name__ == '__main__': try: sys.exit(main()) diff --git a/intrsc.py b/intrsc.py new file mode 100644 index 0000000..564faaa --- /dev/null +++ b/intrsc.py @@ -0,0 +1,52 @@ +from math import sin, cos, tan +from operator import itemgetter + +import Image, ImageDraw + +def board(image, lines, show_all, do_something): + 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: + do_something(image_g, "intersections") + + board = [] + + for line in intersections: + board.append([stone_color(image, intersection) for intersection in + line]) + return board + +def intersections_from_angl_dist(lines, size): + intersections = [] + for (angl1, dist1) in sorted(lines[1], key=itemgetter(1)): + line = [] + for (angl2, dist2) in sorted(lines[0], key=itemgetter(1)): + 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))) + intersections.append(line) + return intersections + +def stone_color(image, (x, y)): + suma = 0. + for i in range(-2, 3): + for j in range(-2, 3): + try: + suma += sum(image.getpixel((x + i, y + j))) + except IndexError: + pass + suma /= 3 * 25 + if suma < 55: + return 'B' + elif suma < 200: + return '.' + else: + return 'W' diff --git a/linef.py b/linef.py index 1dd35d4..fbd1368 100755 --- a/linef.py +++ b/linef.py @@ -100,6 +100,12 @@ def find_lines(image, show_all, do_something, verbose): if show_all: do_something(im_c, "hough x lines") + image_g = image.copy() + draw = ImageDraw.Draw(image_g) + for line in [l for s in lines for l in s]: + draw.line(line_from_angl_dist(line, image.size), fill=(120, 255, 120)) + if show_all: + do_something(image_g, "the grid") return lines diff --git a/manual.py b/manual.py index c7f8e2b..c5b702d 100644 --- a/manual.py +++ b/manual.py @@ -11,7 +11,7 @@ class Screen: def __init__(self, res): pygame.init() pygame.display.set_mode(res) - pygame.display.set_caption("Go image capture") + pygame.display.set_caption("Imago manual mode") self._screen = pygame.display.get_surface() def display_picture(self, im): -- 2.4.2