def convert(self, image): if self.w or self.h: if self.w: w = int(self.w) else: w = image.size[0] if self.h: h = int(self.h) else: h = image.size[1] im = image.resize((w, h)) else: (w, h) = image.size im = image tmp = "" index = 0 for i in range(h): for j in range(w): px = im.getpixel((j, i)) tmp = tmp + '\\e[48;5;' + str( x256.from_rgb(px[0], px[1], px[2])) + "m" + " " index += 1 tmp = tmp + '\\e[0m' tmp = tmp + "\n" return tmp
def renderify(filename, cols=80): """ Render images on the terminal with xterm 256 colors. """ try: url = urllib.urlopen(filename) im = StringIO(url.read()) im = Image.open(im) url.close() except: im = Image.open(filename) im.mode == 'RGBA' width, height = im.size dx = float(width) / cols dy = 2 * dx pixels = im.load() buff = '' y = 0 while (y < height): x = 0 while (x < width): i = round(x - 0.5) if x != 0 else x j = round(y - 0.5) if y != 0 else y if (len(pixels[i, j]) == 3) or (pixels[i, j][3] > 0): color = x256.from_rgb(list(pixels[i, j])[0:3]) buff += '\033[48;5;%dm \033[0m' % color else: buff += ' ' x += dx buff += '\n' y += dy return buff
def plot_ui(): """ Draw pretty lines """ print "\033[1;1H╒\033[1;100H╕\033[35;1H╘\033[35;100H╛" for gi in range(2, 100): print("\033[1;%dH═" % (gi)) print("\033[3;%dH─" % (gi)) print("\033[8;%dH═" % gi) print("\033[29;%dH═" % gi) print("\033[33;%dH═" % gi) print("\033[35;%dH═" % gi) for gy in range(2, 35): print("\033[%d;1H│\033[%d;100H│" % (gy, gy)) print "\033[33;1H╞\033[8;1H╞\033[29;1H╞\033[3;1H├\033[3;100H┤\033[8;100H╡\033[33;100H╡\033[29;100H╡" print "\033[41;66H│" print("\033[42;100H\r\n") ix = x256.from_rgb(160, 130, 10) print "\033[2;3H\x1b[38;5;" + str( ix) + "m xDevs.com TEC Experiment kit \033[0;49m" if cfg.get('teckit', 'interface', 1) == 'gpib': print "\033[2;29H USB-GPIB" elif cfg.get('teckit', 'interface', 1) == 'vxi': print "\033[2;29H LAN-VXI " else: print "No interface defined!" quit()
def print_(self, pixels): y = "" for pixel in pixels: x = pixel.get_colors() y += ('%s %s' % (bg(x256.from_rgb(x[0], x[1], x[2])), attr(0))) sys.stdout.write(y) sys.stdout.flush() self.restart_line()
def print_row(self, y, color=False): # Monochrome: if not color: print(''.join(self.state[y])) return # Spicy: out = [] row = self.state[y] for x in range(len(row)): cell = row[x] if cell == 'G' or cell == 'E': npc = self.objects[Point(x, y)] health = npc.hp / 200.0 green = health red = 1 - health ansi = x256.from_rgb(int(red * 255), int(green * 255), 0) out.append("%s%s%s" % (sty.fg(ansi), cell, sty.fg.rs)) else: out.append(cell) print(''.join(out))
def convert_to_ansi(input_stream, output_stream): image = Image.open(input_stream) image = image.convert('RGBA') output = '' # Calculate relative pixel size based on bounding box # See: https://github.com/dfrankland/image-xterm-loader/blob/master/src/index.js#L25 dx = image.width / OUTPUT_MAX_WIDTH dy = 2 * dx # print("Image size:", image.width, image.height) # print("Relative pixel size:", dx, dy) # print(image.mode) # Start reading the image pixel-by-pixel, incrementing by the relative pixel size # We decrement the width by a single relative pixel size # to prevent reading out-of-bounds of the image. x, y = 0, 0 while y < math.floor(image.height - dy): while x < math.floor(image.width - dx): x += dx pixel = image.getpixel((x, y)) if pixel[3] > 0: # @TODO Refactor x256 core bits into a single file? ansi_colour = '\x1b[48;5;' + str( x256.from_rgb(pixel[0], pixel[1], pixel[2])) + 'm' output += f'{ansi_colour} {ANSI_RESET}' else: output += f' {ANSI_RESET}' # Next vertical row, reset horizontal index to 0 x, y = 0, y + dy # Reset colors and force new row output = f"{output}{ANSI_RESET} \r\n" # Print output to stdout output_stream.write(output)
def test_from_rgb(self): color = x256.from_rgb(self.rgb) self.assertEqual(self.xcolor, color) color = x256.from_rgb(self.rgb[0], self.rgb[1], self.rgb[2]) self.assertEqual(self.xcolor, color)
def get_256_cell(r, g, b): return u'\u001b[38;5;{}m{}'.format(x256.from_rgb(r, g, b), STORED_CELL_CHAR)
def html2xterm256(color): r, g, b = Color.HtmlToRgb(html_color) r = int(r * 255) g = int(g * 255) b = int(b * 255) return x256.from_rgb(r, g, b)