Skip to content

Instantly share code, notes, and snippets.

@ebarns
Last active October 28, 2018 18:33
Show Gist options
  • Save ebarns/1a8f6ea040b4ec0cb75784c54e5effe7 to your computer and use it in GitHub Desktop.
Save ebarns/1a8f6ea040b4ec0cb75784c54e5effe7 to your computer and use it in GitHub Desktop.
from SimpleCV import Camera
class AsciiBoi:
def __init__(self):
pass
def preProcess(self, img):
edge_img = img.edges(30, 60)
edge_img = edge_img.grayscale()
return edge_img
def getRois(self, img, scale):
rois = []
numpy_img = self.preProcess(img).getNumpy()
for col_id in range(int(len(numpy_img) / scale) - 1):
region_low = col_id * scale
region_high = (col_id + 1) * scale
num_col_region = range(int(len(numpy_img[0]) / scale) - 1)
rois += list(
map(lambda row_idx: ROI(numpy_img[region_low:region_high, row_idx * scale:(row_idx + 1) * scale],
region_low, region_high, row_idx * scale, (row_idx + 1) * scale),
num_col_region))
return rois
class ROI:
def __init__(self, region, col_low, col_high, row_low, row_high):
self.row_high = row_high
self.row_low = row_low
self.col_high = col_high
self.col_low = col_low
self.region = region
def img_to_ascii(img, roi_size):
asciboi = AsciiBoi()
ascii_image = [[" " for y in range(int(img.width / roi_size))] for x in range(int(img.height / roi_size))]
regions = asciboi.getRois(img, roi_size)
for roi in regions:
if 255 in roi.region:
ascii_image[int(roi.row_low / roi_size)][int(roi.col_low / roi_size)] = "8"
return ascii_image
def write_to_file(ascii_data):
with open("asciiboi2.txt", "w") as output:
for row in ascii_data:
output.write("".join(row) + "\n")
cam = Camera()
image = cam.getImage()
image = image.scale(2.0)
PIXEL_REGION_SIZE = 6
write_to_file(img_to_ascii(image.copy(), PIXEL_REGION_SIZE))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment