epd_4in37g_test.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/python
  2. # -*- coding:utf-8 -*-
  3. import sys
  4. import os
  5. picdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'pic')
  6. libdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'lib')
  7. if os.path.exists(libdir):
  8. sys.path.append(libdir)
  9. import logging
  10. from waveshare_epd import epd4in37g
  11. import time
  12. from PIL import Image,ImageDraw,ImageFont
  13. import traceback
  14. logging.basicConfig(level=logging.DEBUG)
  15. try:
  16. logging.info("epd4in37g Demo")
  17. epd = epd4in37g.EPD()
  18. logging.info("init and Clear")
  19. epd.init()
  20. epd.Clear()
  21. font24 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 24)
  22. font18 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 18)
  23. font40 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 40)
  24. # Drawing on the image
  25. logging.info("1.Drawing on the image...")
  26. Himage = Image.new('RGB', (epd.width, epd.height), epd.WHITE) # 255: clear the frame
  27. draw = ImageDraw.Draw(Himage)
  28. draw.text((5, 0), 'hello world', font = font18, fill = epd.RED)
  29. draw.text((5, 20), '4.37inch e-Paper', font = font24, fill = epd.YELLOW)
  30. draw.text((5, 45), u'微雪电子', font = font40, fill = epd.BLACK)
  31. draw.text((5, 85), u'微雪电子', font = font40, fill = epd.YELLOW)
  32. draw.text((5, 125), u'微雪电子', font = font40, fill = epd.RED)
  33. draw.line((5, 170, 80, 245), fill = epd.RED)
  34. draw.line((80, 170, 5, 245), fill = epd.YELLOW)
  35. draw.rectangle((5, 170, 80, 245), outline = epd.BLACK)
  36. draw.rectangle((90, 170, 165, 245), fill = epd.YELLOW)
  37. draw.arc((5, 250, 80, 325), 0, 360, fill = epd.BLACK)
  38. draw.chord((90, 250, 165, 325), 0, 360, fill = epd.RED)
  39. epd.display(epd.getbuffer(Himage))
  40. time.sleep(3)
  41. # read bmp file
  42. logging.info("2.read image file")
  43. Himage = Image.open(os.path.join(picdir, '4in37g0.jpg'))
  44. epd.display(epd.getbuffer(Himage))
  45. time.sleep(3)
  46. logging.info("3.read image file")
  47. Himage = Image.open(os.path.join(picdir, '4in37g1.jpg'))
  48. epd.display(epd.getbuffer(Himage))
  49. time.sleep(3)
  50. logging.info("4.read image file")
  51. Himage = Image.open(os.path.join(picdir, '4in37g2.jpg'))
  52. epd.display(epd.getbuffer(Himage))
  53. time.sleep(3)
  54. logging.info("Clear...")
  55. epd.Clear()
  56. logging.info("Goto Sleep...")
  57. epd.sleep()
  58. except IOError as e:
  59. logging.info(e)
  60. except KeyboardInterrupt:
  61. logging.info("ctrl + c:")
  62. epd4in37g.epdconfig.module_exit()
  63. exit()