Created
April 26, 2013 17:10
-
-
Save mplewis/5468785 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Image | |
from collections import Counter | |
IMAGE_FILE = 'testris_fb.png' | |
def get_tetris_vert_bounds_from_img(img): | |
'''Returns the left and right crop boundaries of an image of the player's Tetris Battle board.''' | |
img_width, img_height = img.size | |
left_tri_black_coords = [] | |
for row_num in xrange(img_height): | |
num_black_pixels = 0 | |
found_black_pixel = False | |
for col_num in xrange(img_width): | |
if img.getpixel((col_num, row_num)) == (0, 0, 0): | |
if not found_black_pixel: | |
found_black_pixel = True | |
num_black_pixels += 1 | |
else: | |
if num_black_pixels == 6: | |
start_col = col_num - 6 | |
end_col = col_num - 1 | |
left_tri_black_coords.append(start_col) | |
found_black_pixel = False | |
num_black_pixels = 0 | |
left_tri_black_count = Counter(left_tri_black_coords) | |
inner_board_bound_tuples = left_tri_black_count.most_common(6) | |
inner_board_bounds = [] | |
for coord, count in inner_board_bound_tuples: | |
inner_board_bounds.append(coord) | |
inner_board_bounds.sort() | |
inner_board_bounds = inner_board_bounds[:2] | |
left_bound_no_border = inner_board_bounds[0] + 6 | |
right_bound_no_border = inner_board_bounds[1] - 1 | |
return (left_bound_no_border, right_bound_no_border) | |
def get_tetris_horiz_bounds_from_img(img): | |
'''Returns the top and bottom crop boundaries of an image of the player's Tetris Battle board.''' | |
img_width, img_height = img.size | |
top_tri_black_coords = [] | |
for col_num in xrange(img_width): | |
num_black_pixels = 0 | |
found_black_pixel = False | |
for row_num in xrange(img_height): | |
if img.getpixel((col_num, row_num)) == (0, 0, 0): | |
if not found_black_pixel: | |
found_black_pixel = True | |
num_black_pixels += 1 | |
else: | |
if num_black_pixels == 6: | |
start_row = row_num - 6 | |
end_row = row_num - 1 | |
top_tri_black_coords.append(start_row) | |
found_black_pixel = False | |
num_black_pixels = 0 | |
top_tri_black_count = Counter(top_tri_black_coords) | |
inner_board_bound_tuples = top_tri_black_count.most_common(2) | |
inner_board_bounds = [] | |
for coord, count in inner_board_bound_tuples: | |
inner_board_bounds.append(coord) | |
inner_board_bounds.sort() | |
top_bound_no_border = inner_board_bounds[0] + 6 | |
bottom_bound_no_border = inner_board_bounds[1] - 1 | |
return (top_bound_no_border, bottom_bound_no_border) | |
tetris_img = Image.open(IMAGE_FILE) | |
print 'Getting horizontal boundaries...' | |
vert_bounds = get_tetris_vert_bounds_from_img(tetris_img) | |
print 'Done! Vertical bounds:', vert_bounds | |
print 'Getting vertical boundaries...' | |
horiz_bounds = get_tetris_horiz_bounds_from_img(tetris_img) | |
print 'Done! Horizontal bounds:', horiz_bounds | |
left_bound, right_bound = vert_bounds | |
top_bound, bottom_bound = horiz_bounds | |
tetris_img_width, tetris_img_height = tetris_img.size | |
cropped = tetris_img.crop((left_bound, top_bound, right_bound, bottom_bound)) | |
cropped.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment