import os as _os fontpath = _os.path.realpath(_os.path.join(__file__,"..","..","wavesharelib","pic", 'Font.ttc')) def eink_qr(args): from waveshare_epd import epd2in13_V2 from PIL import Image,ImageDraw import qrcode epd = epd2in13_V2.EPD() epd.init(epd.FULL_UPDATE) image = Image.new('1', (epd.height, epd.width), 0) qr = qrcode.QRCode(version=1, box_size=3, border=1) qr.add_data(args.text) qr.make() pic = qr.make_image().get_image() image.paste(pic, (int(epd.height-pic.width),0), pic) epd.display(epd.getbuffer(image)) epd.sleep() epd.Dev_exit() def eink_qr_shorturl(args): import simpilib args.text = simpilib.short_url(args.text) eink_qr(args) def eink_quote_qr(args): import simpilib import random data = simpilib.getRedditJson("quotes","top","day") quotes = data["data"]["children"] q = random.choice(quotes) # args.text = q["data"]["title"] # eink_text(args) text = q["data"]["title"] url = simpilib.short_url(q["data"]["url"]) smalltext = None import re dashes = r"(.*)[\u002D\u058A\u05BE\u1400\u1806\u2010-\u2015\u2E17\u2E1A\u2E3A\u2E3B\u2E40\u301C\u3030\u30A0\uFE31\uFE32\uFE58\uFE63\uFF0D](.*)" matches = re.match(dashes, text) if matches: groups = matches.groups() if len(groups[1]) < 30: text = groups[0] smalltext = groups[1] if not smalltext: qt = r"(.*)\"(.*)\"(.*)" matches = re.match(qt, text) if matches: groups = matches.groups() if len(groups[0]) < 10 and len(groups[1]) > 10: text = groups[1] smalltext = groups[2].strip() print(text) print(smalltext) eink_text_qr(text, url, smalltext) def eink_text_qr(text, qrdata, smalltext=None): import simpilib from waveshare_epd import epd2in13_V2 from PIL import Image,ImageDraw import qrcode epd = epd2in13_V2.EPD() base_image = Image.new('1', (epd.height, epd.width), 0) qr = qrcode.QRCode(version=1, box_size=3, border=1) qr.add_data(qrdata) qr.make() pic = qr.make_image().get_image() text_image = (base_image.width-pic.width, base_image.height) small_image = (pic.width, base_image.height-pic.height) base_image.paste(pic, (base_image.width-pic.width,0), pic) draw = ImageDraw.Draw(base_image) epd.init(epd.FULL_UPDATE) epd.displayPartBaseImage(epd.getbuffer(base_image)) epd.init(epd.PART_UPDATE) text, font = simpilib.fitText(text_image, fontpath, text) draw.text((0,0), text, align="center", font = font, fill = 255, spacing=2) if smalltext != None and len(smalltext) > 2: smalltext, smallfont = simpilib.fitText(small_image, fontpath, smalltext) draw.text((base_image.width-pic.width, pic.height), smalltext, align="center", font = font, fill = 255, spacing=2) epd.displayPartial(epd.getbuffer(base_image)) epd.sleep() epd.Dev_exit() def eink_quote(args): import simpilib quote = simpilib.getQuote() if not quote: return args.text = quote["content"] + ' - ' + quote["author"] eink_text(args) def eink_text(args): from waveshare_epd import epd2in13_V2 from PIL import Image,ImageDraw import simpilib epd = epd2in13_V2.EPD() print("init and Clear") epd.init(epd.FULL_UPDATE) # epd.Clear(0xFF) image = Image.new('1', (epd.height, epd.width), 0) draw = ImageDraw.Draw(image) text, font = simpilib.fitText(image.size, fontpath, args.text) w, h = draw.textsize(text, font=font) # epd parameters are rotated 90° center = [ int((epd.height - w)/2), int((epd.width-h)/2) - 3, ] # -3 px to leave some space for low characters print("Drawing:", (w, h), (epd.height, epd.width), center) draw.text(center, text, align="center", font = font, fill = 255, spacing=2) # epd.display(epd.getbuffer(image.rotate(180))) epd.display(epd.getbuffer(image)) epd.sleep() epd.Dev_exit()