You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2.2 KiB

aalib PDF

Making an ASCII art PDF using aalib, PIL, and reportlab

In [ ]:
from PIL import Image
from urllib.request import urlopen
import aalib

f = urlopen("https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fkinneretstern.files.wordpress.com%2F2015%2F12%2Flee-miller2.jpg&f=1&nofb=1")
im = Image.open(f)
screen = aalib.AsciiScreen(width=200, height=160)
resized = im.convert("L").resize(screen.virtual_size)
screen.put_image((0, 0), resized)
lines = screen.render().splitlines()
In [ ]:
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.pagesizes import *
from reportlab.lib.units import mm
from reportlab.pdfbase.ttfonts import TTFont, pdfmetrics

c = Canvas("aa.pdf", pagesize=A2, bottomup=0) 
fontpath = "fonts/mplus-1m-regular.ttf"
font =  TTFont('1mregular', fontpath)
pdfmetrics.registerFont(font)
c.setFont('1mregular', 14.4)

start_y = 0*mm
y = start_y
lineheight = 4*mm

for line in lines:
    c.drawString(2*mm, y, line)
    y += lineheight

c.save()
In [ ]:
!pdfinfo aa.pdf
In [ ]: