Raspberry pi & waveshare 2.7 e-ink Display & python

Here’s some example python code for drawing text horizontal on the waveshare e-ink display. You’ll find also a example where to find the length of text where to break lines. This examples reads some small text from a web site and looks up a rss feed for the weather.

Install the fonts https://www.raspberrypi.org/forums/viewtopic.php?t=133772

Install libraries https://www.waveshare.com/wiki/Pioneer600#Libraries_Installation_for_RPi

ToDo: Code format is broken


# coding: utf8
##
 #  width 176
 #  height 264
 ##

import epd2in7
from PIL import Image
import ImageFont
from PIL import ImageDraw
import time
import urllib2
import feedparser
import datetime

def getLengthText(myTextInput, myFont, pixels):
    #font = ImageFont.truetype('/usr/share/fonts/truetype/freefont/msttcorefonts/comic.ttf', 28)
    #font = ImageFont.truetype("arial", 20)
    myText = myTextInput
    mlen = 0
    mlen = len(myText)
    w, h = myFont.getsize(myText)
    while pixels < w:
        myText = myText[0:mlen-2]
        mlen = len(myText)
        w, h = myFont.getsize(myText)
    return mlen

def addText(myImage,myText, myFont, xx,yy):
    #font = ImageFont.truetype('/usr/share/fonts/truetype/freefont/msttcorefonts/comic.ttf', 28)
    #font = ImageFont.truetype("arial", 20)
    w, h = myFont.getsize(myText)
    mask = Image.new('1', (w, h), 255)
    draw2 = ImageDraw.Draw(mask)
    draw2.text((0, 0), myText, font = myFont, fill = 0)
    mask = mask.rotate(90, expand=True)
    myImage.paste(mask, (xx,epd2in7.EPD_HEIGHT - w - yy ))

def main():
    epd = epd2in7.EPD()
    epd.init()

    image = Image.new('1', (epd2in7.EPD_WIDTH, epd2in7.EPD_HEIGHT), 255)    # 255: clear the image with white
    draw = ImageDraw.Draw(image)
    
    font = ImageFont.truetype("arial", 20)
    font2 = ImageFont.truetype("comic", 24)
    font3 = ImageFont.truetype("arial", 28)

    # get text from a feed
    #d = feedparser.parse('http://www.reddit.com/r/news/.rss')
    d = feedparser.parse('https://www.br.de/wetter/action/feeds/bayernwetter.do')
    dnr = 0
    dlen = len(d['entries'])
    #test
    oberbayern = 0
    for n in range(dlen):
        actentry = d['entries'][n]['title'].encode('utf-8').strip()
        print str(n) + " " + actentry[0:8]
        if actentry[0:8] == "Oberbaye":
            oberbayern = n
        #münchen
        if actentry[0:8] == "München":
            muenchen = n
    
    
    print d['entries'][oberbayern]['title'].encode('utf-8').strip()
    print d['entries'][muenchen]['title'].encode('utf-8').strip()
    nb = d['entries'][oberbayern]['title'].strip()
    
    # get text from a website
    ttext = "ghjk"
    try:
            response = urllib2.urlopen('http://4johannes.de/weather/lowest.txt')
            ttext = response.read().strip()
            print(ttext)
    except:
            print("fehler2")

    # add to display

    # first Line
    grad = u'°'
    #entlang umgerechnet oben 1,1 nach 1,264/2
    draw.line((0, 264, 0, 0), fill = 0)
    draw.line((1, 264, 1, 0), fill = 0)
    addText(image, datetime.datetime.now().strftime("%H:%M %d.%m."), font3, 5, 6)
    addText(image, ttext + grad, font3, 5, 182)
    draw.line((42, 264, 42, 0), fill = 0)
    draw.line((43, 264, 43, 0), fill = 0)
    # senkrechte Linien, wenn quer gesehen
    draw.line((0, 264-180+5, 43, 264-180+5), fill = 0)
    draw.line((0, 264-180+4, 43, 264-180+4), fill = 0)
    draw.line((0, 263, 43, 263), fill = 0)
    draw.line((0, 262, 43, 262), fill = 0)

    draw.line((0, 0, 43, 0), fill = 0)
    draw.line((0, 1, 43, 1), fill = 0)


    muc = "München"
    mstr = d['entries'][muenchen]['title'].strip()
    mstr2 = mstr[mstr.find("):")+3:]
    addText(image, mstr2, font, 50, 0)
    #addText(image, "012345678901234567890123456789", font, 120, 0)
    print(nb.encode('utf-8'))
    xlen = 0
    xlen = getLengthText(nb,font, 264)
    nb1 = nb[0:xlen]
    nbRest = nb[xlen:]
    xlen = getLengthText(nbRest,font, 264)
    nb2 = nbRest[0:xlen]
    nbRest = nbRest[xlen:]
    xlen = getLengthText(nbRest,font, 264)
    nb3 = nbRest[0:xlen]
    nbRest = nbRest[xlen:]
    xlen = getLengthText(nbRest,font, 264)
    nb4 = nbRest[0:xlen]
    nbRest = nbRest[xlen:]
    xlen = getLengthText(nbRest,font, 264)
    nb5 = nbRest[0:xlen]
    
    print(nb1.encode('utf-8'))
    print(nb2.encode('utf-8'))
    print(nb3.encode('utf-8'))
    print(nb4.encode('utf-8'))
    print(nb5.encode('utf-8'))
    addText(image, nb1, font, 80, 0)
    addText(image, nb2, font, 100, 0)
    addText(image, nb3, font, 120, 0)
    addText(image, nb4, font, 140, 0)
    addText(image, nb5, font, 140, 0)


    epd.display_frame(epd.get_frame_buffer(image))
    # display images
    #epd.display_frame(epd.get_frame_buffer(Image.open('demopill2.bmp')))

if __name__ == '__main__':
    main()

Leave a Reply

Your email address will not be published. Required fields are marked *