"""Python challenge #14:
http://www.pythonchallenge.com/pc/return/italy.html
"""
import urllib2
from PIL import Image, ImageDraw
un = 'huge'
pw = 'file'
url = 'http://www.pythonchallenge.com/pc/return/wire.png'


def main():
    setup_auth_handler()
    img = urllib2.urlopen(url)
    im = Image.open(img)
    png_matrix = im.load()
    new_img = Image.new('RGB', (100, 100))
    draw = ImageDraw.Draw(new_img)
    for x in xrange(int(im.size[0])):
        draw.point([x % 100, x//100], png_matrix[x, 0])
    new_img.show()


def setup_auth_handler():
    """Method for setting up authentication."""
    password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
    password_mgr.add_password(None, url, un, pw)
    handler = urllib2.HTTPBasicAuthHandler(password_mgr)
    opener = urllib2.build_opener(handler)
    urllib2.install_opener(opener)


if __name__ == '__main__':
    main()

@@ -70,16 +70,69 @@
 ly.html%0A
+-%3E%0Ahttp://www.pythonchallenge.com/pc/return/cat.html%0A
 %22%22%22%0Aimpo
@@ -257,16 +257,25 @@
 re.png'%0A
+dim = 99%0A
 %0A%0Adef ma
@@ -474,13 +474,113 @@
     
-for x
+x = 0       # counter or x in original image%0A    for count, line in enumerate(pattern()):%0A        for num
  in 
@@ -590,34 +590,518 @@
 nge(
+l
 in
-t(im.size%5B0%5D)):%0A
+e):%0A            # draw line%0A            # remainder: 0 top, 1 left, 2 bottom, 3 right%0A            remainder = count %25 4%0A            level = count//4%0A            if remainder == 0:      # top of square%0A                draw.point(%5Bnum+level, level%5D, png_matrix%5Bx, 0%5D)%0A                x += 1%0A            elif remainder == 1:    # right side of square%0A                draw.point(%5Bdim-level, num+level+1%5D, png_matrix%5Bx, 0%5D)%0A                x += 1%0A            elif remainder == 2:    # bottom of square%0A  
         
 draw
@@ -1088,32 +1088,38 @@
 quare%0A          
+      
 draw.point(%5Bx %25 
@@ -1118,62 +1118,471 @@
 nt(%5B
-x %25 100, x//100%5D, png_matrix%5Bx, 0%5D)%0A    new_img.show()
+dim-level-num-1, dim-level%5D, png_matrix%5Bx, 0%5D)%0A                x += 1%0A            else:                   # left side of square%0A                draw.point(%5Blevel, dim-1-num%5D, png_matrix%5Bx, 0%5D)%0A                x += 1%0A    new_img.show()%0A%0A%0Adef pattern():%0A    %22%22%22Define pattern spiral pattern, each number represents an edge.%22%22%22%0A    x = range(101)%0A    x = x%5B::-1%5D%0A    output = %5B%5D%0A    for i in x:%0A        output.append(i)%0A        output.append(i)%0A    return output%5B1:%5D
 %0A%0A%0Ad

solve 14