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