Pages: [1]   Go Down
Author Topic: Yet another picture converter for KS0108  (Read 611 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Faraday Member
**
Karma: 24
Posts: 3486
20 LEDs are enough
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#!/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 smiley
Logged

Check out my experiments http://blog.blinkenlight.net

Pages: [1]   Go Up
Jump to: