eink.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import os as _os
  2. fontpath = _os.path.realpath(_os.path.join(__file__,"..","..","wavesharelib","pic", 'Font.ttc'))
  3. def eink_qr(args):
  4. from waveshare_epd import epd2in13_V2
  5. from PIL import Image,ImageDraw
  6. import qrcode
  7. epd = epd2in13_V2.EPD()
  8. epd.init(epd.FULL_UPDATE)
  9. image = Image.new('1', (epd.height, epd.width), 0)
  10. qr = qrcode.QRCode(version=1, box_size=3, border=1)
  11. qr.add_data(args.text)
  12. qr.make()
  13. pic = qr.make_image().get_image()
  14. image.paste(pic, (int(epd.height-pic.width),0), pic)
  15. epd.display(epd.getbuffer(image))
  16. epd.sleep()
  17. epd.Dev_exit()
  18. def eink_qr_shorturl(args):
  19. import simpilib
  20. args.text = simpilib.short_url(args.text)
  21. eink_qr(args)
  22. def eink_quote_qr(args):
  23. import simpilib
  24. import random
  25. data = simpilib.getRedditJson("quotes","top","day")
  26. quotes = data["data"]["children"]
  27. q = random.choice(quotes)
  28. # args.text = q["data"]["title"]
  29. # eink_text(args)
  30. text = q["data"]["title"]
  31. author = None
  32. import re
  33. dashes = r"(.*)[\u002D\u058A\u05BE\u1400\u1806\u2010-\u2015\u2E17\u2E1A\u2E3A\u2E3B\u2E40\u301C\u3030\u30A0\uFE31\uFE32\uFE58\uFE63\uFF0D](.*)"
  34. matches = re.match(dashes, text)
  35. if matches:
  36. groups = matches.groups()
  37. if len(groups[1]) < 30:
  38. text = groups[0]
  39. author = groups[1]
  40. if not author:
  41. qt = r"(.*)\"(.*)\"(.*)"
  42. matches = re.match(qt, text)
  43. if matches:
  44. groups = matches.groups()
  45. if len(groups[0]) < 10 and len(groups[1]) > 10:
  46. text = groups[1]
  47. author = groups[2]
  48. if author:
  49. author = re.sub(r"[^a-zA-Z0-9 ]","", author).strip()
  50. from urllib.parse import urlencode
  51. args = urlencode({
  52. "search": author,
  53. "title": "Special:Search",
  54. })
  55. url = simpilib.short_url("https://wikipedia.org/w/index.php?" + args)
  56. else:
  57. url = simpilib.short_url(q["data"]["url"])
  58. print(text)
  59. print(author)
  60. eink_text_qr(text, url, author)
  61. def eink_text_qr(text, qrdata, smalltext=None):
  62. import simpilib
  63. from waveshare_epd import epd2in13_V2
  64. from PIL import Image,ImageDraw
  65. import qrcode
  66. epd = epd2in13_V2.EPD()
  67. base_image = Image.new('1', (epd.height, epd.width), 0)
  68. qr = qrcode.QRCode(version=1, box_size=3, border=1)
  69. qr.add_data(qrdata)
  70. qr.make()
  71. pic = qr.make_image().get_image()
  72. text_image = (base_image.width-pic.width, base_image.height)
  73. small_image = (pic.width, base_image.height-pic.height)
  74. base_image.paste(pic, (base_image.width-pic.width,0), pic)
  75. draw = ImageDraw.Draw(base_image)
  76. epd.init(epd.FULL_UPDATE)
  77. epd.displayPartBaseImage(epd.getbuffer(base_image))
  78. epd.init(epd.PART_UPDATE)
  79. text, font = simpilib.fitText(text_image, fontpath, text)
  80. draw.text((0,0), text, align="center", font = font, fill = 255, spacing=2)
  81. if smalltext != None and len(smalltext) > 2:
  82. smalltext, smallfont = simpilib.fitText(small_image, fontpath, smalltext)
  83. draw.text((base_image.width-pic.width, pic.height), smalltext, align="center", font = font, fill = 255, spacing=2)
  84. epd.displayPartial(epd.getbuffer(base_image))
  85. epd.sleep()
  86. epd.Dev_exit()
  87. def eink_quote(args):
  88. import simpilib
  89. quote = simpilib.getQuote()
  90. if not quote:
  91. return
  92. args.text = quote["content"] + ' - ' + quote["author"]
  93. eink_text(args)
  94. def eink_text(args):
  95. from waveshare_epd import epd2in13_V2
  96. from PIL import Image,ImageDraw
  97. import simpilib
  98. epd = epd2in13_V2.EPD()
  99. print("init and Clear")
  100. epd.init(epd.FULL_UPDATE)
  101. # epd.Clear(0xFF)
  102. image = Image.new('1', (epd.height, epd.width), 0)
  103. draw = ImageDraw.Draw(image)
  104. text, font = simpilib.fitText(image.size, fontpath, args.text)
  105. w, h = draw.textsize(text, font=font)
  106. # epd parameters are rotated 90°
  107. center = [
  108. int((epd.height - w)/2),
  109. int((epd.width-h)/2) - 3,
  110. ]
  111. # -3 px to leave some space for low characters
  112. print("Drawing:", (w, h), (epd.height, epd.width), center)
  113. draw.text(center, text, align="center", font = font, fill = 255, spacing=2)
  114. # epd.display(epd.getbuffer(image.rotate(180)))
  115. epd.display(epd.getbuffer(image))
  116. epd.sleep()
  117. epd.Dev_exit()