eink.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. url = simpilib.short_url(q["data"]["url"])
  32. smalltext = None
  33. import re
  34. dashes = r"(.*)[\u002D\u058A\u05BE\u1400\u1806\u2010-\u2015\u2E17\u2E1A\u2E3A\u2E3B\u2E40\u301C\u3030\u30A0\uFE31\uFE32\uFE58\uFE63\uFF0D](.*)"
  35. matches = re.match(dashes, text)
  36. if matches:
  37. groups = matches.groups()
  38. if len(groups[1]) < 30:
  39. text = groups[0]
  40. smalltext = groups[1]
  41. if not smalltext:
  42. qt = r"(.*)\"(.*)\"(.*)"
  43. matches = re.match(qt, text)
  44. if matches:
  45. groups = matches.groups()
  46. if len(groups[0]) < 10 and len(groups[1]) > 10:
  47. text = groups[1]
  48. smalltext = groups[2].strip()
  49. print(text)
  50. print(smalltext)
  51. eink_text_qr(text, url, smalltext)
  52. def eink_text_qr(text, qrdata, smalltext=None):
  53. import simpilib
  54. from waveshare_epd import epd2in13_V2
  55. from PIL import Image,ImageDraw
  56. import qrcode
  57. epd = epd2in13_V2.EPD()
  58. base_image = Image.new('1', (epd.height, epd.width), 0)
  59. qr = qrcode.QRCode(version=1, box_size=3, border=1)
  60. qr.add_data(qrdata)
  61. qr.make()
  62. pic = qr.make_image().get_image()
  63. text_image = (base_image.width-pic.width, base_image.height)
  64. small_image = (pic.width, base_image.height-pic.height)
  65. base_image.paste(pic, (base_image.width-pic.width,0), pic)
  66. draw = ImageDraw.Draw(base_image)
  67. epd.init(epd.FULL_UPDATE)
  68. epd.displayPartBaseImage(epd.getbuffer(base_image))
  69. epd.init(epd.PART_UPDATE)
  70. text, font = simpilib.fitText(text_image, fontpath, text)
  71. draw.text((0,0), text, align="center", font = font, fill = 255, spacing=2)
  72. if smalltext != None and len(smalltext) > 2:
  73. smalltext, smallfont = simpilib.fitText(small_image, fontpath, smalltext)
  74. draw.text((base_image.width-pic.width, pic.height), smalltext, align="center", font = font, fill = 255, spacing=2)
  75. epd.displayPartial(epd.getbuffer(base_image))
  76. epd.sleep()
  77. epd.Dev_exit()
  78. def eink_quote(args):
  79. import simpilib
  80. quote = simpilib.getQuote()
  81. if not quote:
  82. return
  83. args.text = quote["content"] + ' - ' + quote["author"]
  84. eink_text(args)
  85. def eink_text(args):
  86. from waveshare_epd import epd2in13_V2
  87. from PIL import Image,ImageDraw
  88. import simpilib
  89. epd = epd2in13_V2.EPD()
  90. print("init and Clear")
  91. epd.init(epd.FULL_UPDATE)
  92. # epd.Clear(0xFF)
  93. image = Image.new('1', (epd.height, epd.width), 0)
  94. draw = ImageDraw.Draw(image)
  95. text, font = simpilib.fitText(image.size, fontpath, args.text)
  96. w, h = draw.textsize(text, font=font)
  97. # epd parameters are rotated 90°
  98. center = [
  99. int((epd.height - w)/2),
  100. int((epd.width-h)/2) - 3,
  101. ]
  102. # -3 px to leave some space for low characters
  103. print("Drawing:", (w, h), (epd.height, epd.width), center)
  104. draw.text(center, text, align="center", font = font, fill = 255, spacing=2)
  105. # epd.display(epd.getbuffer(image.rotate(180)))
  106. epd.display(epd.getbuffer(image))
  107. epd.sleep()
  108. epd.Dev_exit()