#! /usr/bin/env python
# -*- coding: utf-8 -*- 

#============================================================================
# Ejercicio en: http://www.pythonchallenge.com/pc/return/italy.html 
# Solución: cat -> uzi
# Obtenemos: http://www.pythonchallenge.com/pc/return/uzi.html
#----------------------------------------------------------------------------
# FJA - neocipres@telefonica.net            Julio de 2007
#============================================================================

#usuario: huge, password: file
#remember: 100*100 = (100+99+99+98) + (... 

import Image


img = Image.open('wire.png')
data =  list( img.getdata() )
imgout = Image.new( 'RGB', ( 100, 100) )
pix = ()
c = 0
d, l, u, r = 0, 0, 1, 0
x, y = 0, 0
x0, y0, xf, yf = 0, 0, 99, 99 
while 1:
   
    if u:
        pix = img.getpixel( (c, 0) )
        if xf > 49:
            imgout.putpixel( ( x, y ),pix )
            x += 1
        if x == xf:
            d = 0
            l = 0
            u = 0
            r = 1
            #print x0, y0, xf, yf, " - x,y: ", x, y
            #tecla = raw_input("U - Enter ...")
            xf -= 1
            
        
    if r:
        pix = img.getpixel( (c, 0) )
        if  yf > 49:
            imgout.putpixel( ( x, y ),pix )
            y += 1
        if y == yf:
            d = 1
            l = 0
            u = 0
            r = 0
            #print x0, y0, xf, yf, " - ", x, y
            #tecla = raw_input("R - Enter ...")
            yf -= 1
        

    if d:
        pix = img.getpixel( (c, 0) )
        if x0 < 50:
            imgout.putpixel( ( x, y ),pix )
            x -= 1
        if x == x0:
            d = 0
            l = 1
            u = 0
            r = 0
            #print x0, y0, xf, yf, " - ", x, y
            #tecla = raw_input("D - Enter ...")
            x0 += 1
        
     
    if l:
        pix = img.getpixel( (c, 0) )
        if y0 < 50:
            imgout.putpixel( ( x, y ),pix )
            
        if y == y0:
            d = 0
            l = 0
            u = 1
            r = 0
            #print x0, y0, xf, yf, " - x,y: ", x, y
            #tecla = raw_input("L - Enter ...")
            y0 += 1
            
        else:y -= 1
            
        
    c += 1
    if c > len(data)-1:
        break

imgout = imgout.resize((150,150))
imgout.show()
imgout.save("cha14.png", 'PNG')
