No Description

make_oled_pic.py 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env python
  2. # Author: Brendan Le Foll <brendan.le.foll@intel.com>
  3. # Copyright (c) 2014 Intel Corporation.
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining
  6. # a copy of this software and associated documentation files (the
  7. # "Software"), to deal in the Software without restriction, including
  8. # without limitation the rights to use, copy, modify, merge, publish,
  9. # distribute, sublicense, and/or sell copies of the Software, and to
  10. # permit persons to whom the Software is furnished to do so, subject to
  11. # the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be
  14. # included in all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
  23. from __future__ import print_function
  24. from PIL import Image
  25. import sys
  26. # Pixels are arranged in one byte for 8 vertical pixels and not addressed individually
  27. # We convert the image to greysacle and end up making it monochrome where we
  28. # consider that every pixel that is '40' is black.
  29. width = 128
  30. height = 64
  31. if len(sys.argv) != 2:
  32. print('Please specify an image to use as the only argument')
  33. exit(1)
  34. im = Image.open(sys.argv[1])
  35. im = im.convert('L').resize((width, height))
  36. data = list(im.getdata())
  37. byteblock = [0 for i in range(width)]
  38. widthblock = [list(byteblock) for i in range(int(height/8))]
  39. numblock = 0
  40. pixcount = 0
  41. i = 0
  42. # we split the list by width * 8, to create data chunks of 8rows
  43. datachunks=[data[x:x+(width*8)] for x in range(0, len(data), (width*8))]
  44. # grab every pixel of image (or datachunk)
  45. while i < len(widthblock):
  46. pixcount = 0
  47. for y in datachunks[i]:
  48. xcoor = pixcount % width
  49. ycoor = int(pixcount/width)
  50. blknum = xcoor % len(widthblock)
  51. blkycoor = ycoor
  52. # 40 is what we consider 'black'
  53. if y > 40:
  54. widthblock[i][xcoor] |= (1 << blkycoor)
  55. pixcount += 1
  56. i += 1
  57. flatlist = [y for x in widthblock for y in x]
  58. carray = 'static uint8_t image[] = {\n' + ', '.join(str(x) for x in flatlist)
  59. print(carray + '\n};')