def load_arena(): arena = [] baphomet_head = image2term('img/baphomet_head.gif', ratio=0.48, invert=True) tw,th = get_terminal_size_in_pixels() arena.extend(set_pos(baphomet_head,tw/2 - 105,th/2 - 105)) arena.extend(image2term('img/arena.png',ratio=1.0)) return arena
def image2term(image, threshold=128, ratio=None, invert=False): if image not in img_cache: if image.startswith('http://') or image.startswith('https://'): i = Image.open(StringIO(urllib2.urlopen(image).read())).convert('L') else: i = Image.open(open(image)).convert('L') w, h = i.size if ratio: w = int(w * ratio) h = int(h * ratio) i = i.resize((w, h), Image.ANTIALIAS) else: tw,th = get_terminal_size_in_pixels() if tw < w: ratio = tw / float(w) w = tw h = int(h * ratio) if th < h: h = th elif th < h: ratio = th / float(h) h = th w = int(w * ratio) if tw < w: w = tw i = i.resize((w, h), Image.ANTIALIAS) img_cache[image] = i x = y = 0 i = img_cache[image] w,h = i.size frame = [] try: i_converted = i.tobytes() except AttributeError: i_converted = i.tostring() for pix in i_converted: if invert: if ord(pix) > threshold: frame.append((x,y,COLOR_WHITE)) else: if ord(pix) < threshold: frame.append((x,y,COLOR_WHITE)) x += 1 if x >= w: y += 1 x = 0 return frame