import sys
import Image
import numpy

im = Image.open(sys.argv[1])
def isok(px):
    return px != (248, 0, 248, 255)

w = sum([isok(im.getpixel((x,0))) for x in range(im.size[0])])
h = sum([isok(im.getpixel((0,y))) for y in range(im.size[1])])

Xv = numpy.array([isok(im.getpixel((x,0))) for x in range(im.size[0])])
Xi = numpy.where(Xv, (numpy.cumsum(Xv) - 1), -1)
Yv = numpy.array([isok(im.getpixel((0,y))) for y in range(im.size[1])])
Yi = numpy.where(Yv, (numpy.cumsum(Yv) - 1), -1)
print Yi
result = Image.new(im.mode, (w,h))
for i in range(im.size[0]):
    for j in range(im.size[1]):
        dstxy = (int(Xi[i]), int(Yi[j]))
        if -1 < min(dstxy):
            src = im.getpixel((i,j))
            result.putpixel(dstxy, src)
result.save("result.png")
