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.

97 lines
2.2 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# aalib PDF\n",
"\n",
"Making an ASCII art PDF using aalib, PIL, and reportlab"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from PIL import Image\n",
"from urllib.request import urlopen\n",
"import aalib\n",
"\n",
"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\")\n",
"im = Image.open(f)\n",
"screen = aalib.AsciiScreen(width=200, height=160)\n",
"resized = im.convert(\"L\").resize(screen.virtual_size)\n",
"screen.put_image((0, 0), resized)\n",
"lines = screen.render().splitlines()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from reportlab.pdfgen.canvas import Canvas\n",
"from reportlab.lib.pagesizes import *\n",
"from reportlab.lib.units import mm\n",
"from reportlab.pdfbase.ttfonts import TTFont, pdfmetrics\n",
"\n",
"c = Canvas(\"aa.pdf\", pagesize=A2, bottomup=0) \n",
"fontpath = \"fonts/mplus-1m-regular.ttf\"\n",
"font = TTFont('1mregular', fontpath)\n",
"pdfmetrics.registerFont(font)\n",
"c.setFont('1mregular', 14.4)\n",
"\n",
"start_y = 0*mm\n",
"y = start_y\n",
"lineheight = 4*mm\n",
"\n",
"for line in lines:\n",
" c.drawString(2*mm, y, line)\n",
" y += lineheight\n",
"\n",
"c.save()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pdfinfo aa.pdf"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}