#!/usr/bin/python

from PIL import Image
from binascii import hexlify
import sys
import struct


FILE = 'picture.rpp'

def from_image(src):
    im = Image.open(src)
    width = im.size[0]
    height = im.size[1]
    pixels = im.load()

    with open(FILE, 'wb') as f:
        f.write(struct.pack('i', width))
        f.write(struct.pack('i', height))

        for y in range(height):
            for x in range(width):
                f.write(chr(int(pixels[x, y][2])))
                f.write(chr(int(pixels[x, y][1])))
                f.write(chr(int(pixels[x, y][0])))
                f.write('\x00')

        f.close()

if __name__=='__main__':
    if len(sys.argv) < 2:
        print 'Supply a path to an image file to convert'
    else:
        from_image(sys.argv[1])
