1. indexed image to avr pin bitmap

    this python script converts an indexed image to the avr pin bitmap equivalent. uses a special gimp palette that maps indexed colors to the pin bitmap. i.e. for the 28 color gimp palette, a bright red indexed pixel will be converted to the byte 00100100.

    usage: img2avr.py palette.gpl image.png > image.out

    img2avr.py

    # copy-it-right
    
    import sys
    
    from PIL import Image
    
    
    def parse_palette(data):
        palette = []
        after_header = False
        for line in data:
            if line.startswith('#'):
                after_header = True
                continue
            if after_header:
                palette.append(int(line.split()[-1], 2))
        return palette
    
    
    def main():
        color_pin_map = parse_palette(open(sys.argv[1]))
        image = Image.open(sys.argv[2])
    
        assert image.mode == 'P', 'image must be indexed'
    
        sys.stdout.write(chr(int('11111100', 2)))
    
        for i, px in enumerate(image.getdata()):
            sys.stdout.write(chr(color_pin_map[px]))
    
    
    if __name__ == '__main__':
        main()
    

Powered by Tumblr; designed by Adam Lloyd.