epd_4in2bc_test.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/python
  2. # -*- coding:utf-8 -*-
  3. import sys
  4. sys.path.append(r'../lib')
  5. import epd4in2bc
  6. import epdconfig
  7. import time
  8. from PIL import Image,ImageDraw,ImageFont
  9. import traceback
  10. try:
  11. print("epd4in2bc Demo")
  12. epd = epd4in2bc.EPD()
  13. print("init and Clear")
  14. epd.init()
  15. epd.Clear()
  16. time.sleep(1)
  17. # Drawing on the image
  18. print("Drawing")
  19. font24 = ImageFont.truetype('../lib/Font.ttc', 24)
  20. font18 = ImageFont.truetype('../lib/Font.ttc', 18)
  21. # Drawing on the Horizontal image
  22. print("1.Drawing on the Horizontal image...")
  23. HBlackimage = Image.new('1', (epd.width, epd.height), 255) # 298*126
  24. HRYimage = Image.new('1', (epd.width, epd.height), 255) # 298*126 ryimage: red or yellow image
  25. drawblack = ImageDraw.Draw(HBlackimage)
  26. drawry = ImageDraw.Draw(HRYimage)
  27. drawblack.text((10, 0), 'hello world', font = font24, fill = 0)
  28. drawblack.text((10, 20), '4.2inch e-Paper bc', font = font24, fill = 0)
  29. drawblack.text((150, 0), u'微雪电子', font = font24, fill = 0)
  30. drawblack.line((20, 50, 70, 100), fill = 0)
  31. drawblack.line((70, 50, 20, 100), fill = 0)
  32. drawblack.rectangle((20, 50, 70, 100), outline = 0)
  33. drawry.line((165, 50, 165, 100), fill = 0)
  34. drawry.line((140, 75, 190, 75), fill = 0)
  35. drawry.arc((140, 50, 190, 100), 0, 360, fill = 0)
  36. drawry.rectangle((80, 50, 130, 100), fill = 0)
  37. drawry.chord((200, 50, 250, 100), 0, 360, fill = 0)
  38. epd.display(epd.getbuffer(HBlackimage), epd.getbuffer(HRYimage))
  39. time.sleep(2)
  40. # Drawing on the Vertical image
  41. print("2.Drawing on the Vertical image...")
  42. LBlackimage = Image.new('1', (epd.height, epd.width), 255) # 126*298
  43. LRYimage = Image.new('1', (epd.height, epd.width), 255) # 126*298
  44. drawblack = ImageDraw.Draw(LBlackimage)
  45. drawry = ImageDraw.Draw(LRYimage)
  46. drawblack.text((2, 0), 'hello world', font = font18, fill = 0)
  47. drawblack.text((2, 20), '4.2inch epd bc', font = font18, fill = 0)
  48. drawblack.text((20, 50), u'微雪电子', font = font18, fill = 0)
  49. drawblack.line((10, 90, 60, 140), fill = 0)
  50. drawblack.line((60, 90, 10, 140), fill = 0)
  51. drawblack.rectangle((10, 90, 60, 140), outline = 0)
  52. drawry.line((95, 90, 95, 140), fill = 0)
  53. drawry.line((70, 115, 120, 115), fill = 0)
  54. drawry.arc((70, 90, 120, 140), 0, 360, fill = 0)
  55. drawry.rectangle((10, 150, 60, 200), fill = 0)
  56. drawry.chord((70, 150, 120, 200), 0, 360, fill = 0)
  57. epd.display(epd.getbuffer(LBlackimage), epd.getbuffer(LRYimage))
  58. time.sleep(2)
  59. print("3.read bmp file")
  60. HBlackimage = Image.open('../pic/4in2b-b.bmp')
  61. HRYimage = Image.open('../pic/4in2b-r.bmp')
  62. # HBlackimage = Image.open('../pic/4in2c-b.bmp')
  63. # HRYimage = Image.open('../pic/4in2c-y.bmp')
  64. epd.display(epd.getbuffer(HBlackimage), epd.getbuffer(HRYimage))
  65. time.sleep(2)
  66. print("4.read bmp file on window")
  67. blackimage1 = Image.new('1', (epd.width, epd.height), 255) # 298*126
  68. redimage1 = Image.new('1', (epd.width, epd.height), 255) # 298*126
  69. newimage = Image.open('../pic/100x100.bmp')
  70. blackimage1.paste(newimage, (50,10))
  71. epd.display(epd.getbuffer(blackimage1), epd.getbuffer(redimage1))
  72. print("Clear...")
  73. epd.init()
  74. epd.Clear()
  75. print("Goto Sleep...")
  76. epd.sleep()
  77. except IOError as e:
  78. print(e)
  79. except KeyboardInterrupt:
  80. print("ctrl + c:")
  81. epdconfig.module_exit()
  82. exit()