import cairo
import Image

s = cairo.SVGSurface('foo.svg', 144, 144)
# s = cairo.ImageSurface(cairo.FORMAT_ARGB32, 256, 256)
ctx = cairo.Context(s)

ctx.set_source_rgb(0,0,0)
ctx.set_line_width(0.1)
for i in range(65):
    ctx.move_to(0, 4 * i)
    ctx.line_to(256, 4 * i)
    ctx.move_to(4 * i, 0)
    ctx.line_to(4 * i, 256)
ctx.stroke()
# s.write_to_png('foo.png')
s.finish()

r = Image.open("r.png")
def r1(r):
    return r.transform((16, 16), Image.QUAD, (0,0, 16,0, 16,16, 0,16), Image.NEAREST)
def r2(r):
    return r.transpose(Image.FLIP_LEFT_RIGHT)
def r4(r):
    return r.transpose(Image.FLIP_TOP_BOTTOM)

for i in range(8):
    im = r
    if i & 4:
        im = r4(im)
    if i & 2:
        im = r2(im)
    if i & 1:
        im = r1(im)
    im.save("r%d.png" % i)
