| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- 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"]
- author = 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]
- author = groups[1]
- if not author:
- 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]
- author = groups[2]
- if author:
- author = re.sub(r"[^a-zA-Z0-9 ]","", author).strip()
- from urllib.parse import urlencode
- args = urlencode({
- "search": author,
- "title": "Special:Search",
- })
- url = simpilib.short_url("https://wikipedia.org/w/index.php?" + args)
- else:
- url = simpilib.short_url(q["data"]["url"])
- print(text)
- print(author)
- eink_text_qr(text, url, author)
- 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()
|