OMG! The smallest eBook reader! • 5 Jun 2010

Actually, it's a keychain photo frame I found at the MediaMarkt for a few euros. It comes with a horrible proprietary photo uploading application (of course, Microsoft Windows only), and can't be accessed as a standard USB mass storage device. Nevertheless, it was good fun writing an small Python program for it so that I can use it as an eBook reader.
I give the code without further explanation. The comments should suffice.
# This program uses PIL, the Python Image Library.
# See http://www.pythonware.com/library/pil/handbook/
import Image, ImageFont, ImageDraw
# The USB picture frame can display 128x128 pixels.
dstw = 128
dsth = 128
# A small left margin, and a vertical compression offset
# to squeeze more text on the small display.
margx = 2
compressy = -3
# Read the source text (downloaded by my Gutenberg scraper)
txt = open("pictureframeebook-27961-8.txt").read()
# Split a text into words, spaces and newlines.
def tokenize(s):
word = ""
for c in s:
if c in (" ","\n"):
yield word
yield c
word = ""
else:
word += c
yield word
# Save the current image and create a blank one.
def nextpage(image,pagenr):
fn = "pictureframebook-%03d.jpg" % pagenr
image.save(fn)
print fn
pagenr += 1
image = Image.new("L", (dstw, dsth), 255)
draw = ImageDraw.Draw(image)
return image, draw, pagenr
# Grayscale images will do
image = Image.new("L", (dstw, dsth), 255)
draw = ImageDraw.Draw(image)
# Yeah, yeah, I know. http://bancomicsans.com/
# But it's still my favourite font ;-)
font = ImageFont.truetype("/usr/home/user/fonts/comic.ttf", 11)
x, y = margx, 0
pagenr = 1
spacew, spaceh = font.getsize(" ")
# Distribute all words over pages.
for word in tokenize(txt):
if word == "\n":
# End of paragraph. Start at the left margin again.
if x > margx:
y += h + compressy
if y >= dsth - h + compressy:
# Possibly go to next page, if this one is full.
image, draw, pagenr = nextpage(image, pagenr)
x, y = margx, 0
x = margx
word = ""
w, h = font.getsize(word)
if x + w > dstw:
# The word doesn't fit on this line, go to next line.
x = margx
y += h + compressy
if y >= dsth - h + compressy:
# End of page, go to next page.
image, draw, pagenr = nextpage(image, pagenr)
x, y = margx, 0
draw.text((x-spacew, y), " " + word, font=font)
x += w
# Don't forget to save the last page ;-)
image, pagenr = nextpage(image, pagenr)
Comments
jon • 25 Apr
there is a font called Adobe-Dialog-Small which takes even lesser room, why dont you try that??
zaq • 29 May
Try ProFont http://www.tobias-jung.de/seekingprofont/
burt-B • 7 Jun 2010
Comic takes a lot of pixels. You can find more efficient fonts than that (look at the font that is used to display notes on the iPod nano, for example)