Skip to content

Instantly share code, notes, and snippets.

@steveway
Last active November 15, 2017 16:10
Show Gist options
  • Select an option

  • Save steveway/ed242d030c945d580d108f6195604a53 to your computer and use it in GitHub Desktop.

Select an option

Save steveway/ed242d030c945d580d108f6195604a53 to your computer and use it in GitHub Desktop.
from PIL import Image, ImageDraw, ImageFont, ImageOps
import numpy as np
import random
try:
unichr
except NameError:
unichr = chr
def render(input_arr):
output = input_arr
out_im = Image.new("RGBA", (128,128), color = (0xFF,0xFF,0xFF,0xFF))
d = ImageDraw.Draw(out_im)
fnt = ImageFont.truetype('unifont-10.0.06.ttf', 8, encoding="utf-32")
for charac, i in zip(output, range(len(output))):
x = 8 * int(i %16)
y = 8 * int(i/16)
try:
d.text((x,y),unichr(max(0,min(0x110000,int(charac)))),font=fnt,fill=(0x00,0x00,0x00,0xff))
except:
d.text((x,y)," ",font=fnt,fill=(0x00,0x00,0x00,0xff))
return out_im
def main():
iterations = 100
num_of_chars = 16 * 16
for i in range(iterations):
text_file = open(r"./test_data/%d.txt"%i, "wb")
output_text = []
for a in range(num_of_chars):
test_char = random.randint(0x00, 0x110000)
text_file.write(test_char.to_bytes(4, "big"))
output_text.append(test_char)
out_im = render(output_text)
out_im.convert("L").resize((32,32)).save(r"./test_data/%d.png"%i)
#text_file.write(output_text)
text_file.close()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment