From c13a5fb97c22ae952b4a99e75735f9c96efcf438 Mon Sep 17 00:00:00 2001 From: Tomas Musil Date: Sat, 5 Jan 2013 16:05:21 +0100 Subject: [PATCH 1/1] color distribution plot --- imago.py | 13 ++++++++++++- intrsc.py | 18 +++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/imago.py b/imago.py index 4b87f0f..136574a 100755 --- a/imago.py +++ b/imago.py @@ -94,7 +94,18 @@ def main(): draw.line(l, fill=(64, 255, 64), width=1) do_something(im_g, "grid", name="grid") - board = intrsc.board(image, lines, show_all, do_something) + board, board_raw = intrsc.board(image, lines, show_all, do_something) + + ### Show color distribution + #import matplotlib.pyplot as pyplot + #luma = [(0.30 * s[0] + 0.59 * s[1] + 0.11 * s[2]) / 255. + # for s in sum(board_raw, [])] + #pyplot.scatter(luma, + # [(max(s) - min(s)) / (255 - abs(max(s) + min(s) - 255)) + # for s in sum(board_raw, [])], + # color=[(s[0]/255., s[1]/255., s[2]/255., 1.) for s in sum(board_raw, [])]) + #pyplot.show() + ### #simple ASCII output: for line in board: diff --git a/intrsc.py b/intrsc.py index cefed90..ba36b6d 100644 --- a/intrsc.py +++ b/intrsc.py @@ -34,11 +34,14 @@ def board(image, lines, show_all, do_something): do_something(image_g, "intersections") board_r = [] + board_raw = [] for line in intersections: board_r.append([stone_color(image, intersection) for intersection in line]) - return board_r + board_raw.append([stone_color_raw(image, intersection) for intersection in + line]) + return board_r, board_raw def intersections_from_angl_dist(lines, size, get_all=True): """Take grid-lines and size of the image. Return intersections.""" @@ -73,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 -- 2.4.2