Skip to content

Instantly share code, notes, and snippets.

@Phuket2
Created June 3, 2016 15:47
Show Gist options
  • Select an option

  • Save Phuket2/3b0f30ce463e4b7d3de2346257295c61 to your computer and use it in GitHub Desktop.

Select an option

Save Phuket2/3b0f30ce463e4b7d3de2346257295c61 to your computer and use it in GitHub Desktop.
AsciiLuke.py
import ui, io
def get_max_fontsize(rect, font_name):
r = ui.Rect(*rect)
last_w = last_h = 0
for i in range(5, 1000):
w, h = ui.measure_string('W', max_width=0,
font=(font_name, i), alignment=ui.ALIGN_CENTER,
line_break_mode=ui.LB_TRUNCATE_TAIL)
if w > r.width or h > r.height:
return (i - 1)
last_w, last_h = w, h
def draw_text(rect, text, font_name, font_size, text_color = 'black'):
with ui.GState():
ui.draw_string(text, rect=rect,
font=(font_name, font_size), color=text_color,
alignment=ui.ALIGN_CENTER,
line_break_mode=ui.LB_TRUNCATE_TAIL)
class AsciiRender(ui.View):
def __init__(self, path, *args, **kwargs):
super().__init__(*args, **kwargs)
self.cells = []
self.chars = []
self.cols = 0
self.rows = 0
self.font_size = 0
self.font_name = 'Menlo'
self.text_color = 'black'
# read in Lukes output.txt file, and...
# set the num columns and rows, and...
# add the chars to a list
lines = 0
first_line = True
with io.open(path, encoding = 'utf-8') as file:
for ln in file.readlines():
lst = [*ln]
self.chars += lst[:-1]
if first_line:
self.cols = len(self.chars)
first_line = False
lines += 1
self.rows = lines
self.calc_cells()
self.font_size = get_max_fontsize(self.cells[0], self.font_name)
def calc_cells(self):
cell_w = self.bounds.width / self.cols
cell_h = cell_w #self.bounds.height / self.rows
for i in range(self.rows):
for j in range(self.cols):
self.cells.append((i * cell_w, j * cell_h, cell_w, cell_h))
def draw(self):
# looks stupid to do this. its not really. you are not suppose to
# call draw directly. so if you want to render into an
# image for example you find you have a problem. this way you can
# use set_needs_update or render if you want it capture a image.
# see below , get_image method
self.render()
def render(self):
for i, cell in enumerate(self.cells):
draw_text(cell, self.chars[i], self.font_name, self.font_size, text_color = self.text_color)
def get_image(self):
with ui.ImageContext(self.bounds.width, self.bounds.height) as ctx:
self.render()
return ctx.get_image()
def save_image(self, path = 'testing.png'):
img = self.get_image()
png_bytes = img.to_png()
with io.open(path, 'wb') as file:
file.write(png_bytes)
def shift_chars(self):
for c in self.chars:
c = ord(c) + 5
if __name__ == '__main__':
w = 400
h = 400
f = (0, 0, w, h)
# file_name needs to be the output file from Image2ASCII
# https://github.com/The-Penultimate-Defenestrator/Image2ASCII/blob/master/Image2ASCII.py
file_name = 'output.txt'
ar = AsciiRender(path = 'output.txt', frame = f, bg_color = 'white')
ar.present('sheet')
ar.wait_modal()
ar.shift_chars()
ar.text_color = 'deeppink'
ar.get_image().show()
ar.text_color = 'yellow'
ar.font_name = 'Bodoni Ornaments'
ar.get_image().show()
ar.save_image()
print('finished')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment