Go Down

Topic: Yet another picture converter for KS0108 (Read 630 times) previous topic - next topic

Udo Klein

There exist already converter that pick up graphics and convert them into C files for displaying them with a KS0108. However I did not find what I was searching for within 1h so I decided to put together just another converter. It is in python and hence somewhat platform independent and due to PIL it is not very picky about the input format.
Maybe somebody finds it useful:

Code: [Select]

#!/usr/bin/python
# -*- coding: utf-8 -*-

maxlen = 75

import sys
from PIL import Image

if len(sys.argv) != 2:
   print "Please specify a source filename"
   sys.exit(-1)


sourcefilename = sys.argv[1]
name = sourcefilename.split(".")[0]

image = Image.open(sourcefilename)

# convert image to black and white
image = image.convert("1")

(xsize, ysize) = image.size

print "#include <inttypes.h>"
print "#include <avr/pgmspace.h>"
print
print "#ifndef " + name + "_H"
print "#define " + name + "_H"
print
print "static uint8_t " + name + "Icon[] PROGMEM = {"
print "\t" + str(xsize) + ", // width"
print "\t" + str(ysize) + ", // height"

page = 0
# a page always consists of 8 lines, each byte contains a pixel for each line
# the bytes are ordered from left to rigt
for page in range(0, (ysize + 7)/ 8):
     
     print
     print "\t// page " + str(page)+"  lines" + str(page*8) + "-" + str(page*8+7)
     output = "\t"

     for x in range (0,xsize):
           byte = 0;                        
           for line in range (0,8):
                 # shift the bits into byte for one x coordinate
                 byte+= byte
                 if page*8 + (7-line) < ysize and image.getpixel((x, page*8 + (7-line))) > 0:
                       byte+= 1                        
           output = output + hex(byte) + ", "
           if len(output) >= maxlen and x < xsize - 1:
                 print output
                 output = "\t"

     if page < ((ysize+7) / 8) - 1:
           print output
     else:
           print output[:-2]
           print


print "};"
print "#endif"


Faster to implement than to search around any further :)
Check out my experiments http://blog.blinkenlight.net

Go Up