import urllib2
import base64
import Image

url="http://www.pythonchallenge.com/pc/return/cave.jpg"

auth="<PASSWORD>"
password=base64.b64encode(auth)
tt="Basic "+password
req = urllib2.Request(url)
req.add_header('Authorization',tt)
data = urllib2.urlopen(req).read()

f = open('cave.jpg','wb')
f.write(data)
f.close()

f = Image.open('cave.jpg')
(h,w) = f.size

print h,w
for x in xrange(int(h)):
    for y in xrange(int(w)):
	if y % 2 ==0 and x % 2 ==0:
            try:
                (a1,a2,a3)= f.getpixel((x-1,y))
                (b1,b2,b3)= f.getpixel((x+1,y))
                (c1,c2,c3)= f.getpixel((x,y-1))
                (d1,d2,d3)= f.getpixel((x,y+1))

		z1 = int((a1+b1+c1+d1)/4.0)
		z2 = int((a2+b2+c2+d2)/4.0)
		z3 = int((a3+b3+c3+d3)/4.0)
		#print z1,z2,z3
		try:
                    f.putpixel((x,y),(z1,z2,z3))
		except:
                    print z1,z2,z3,x,y
            except:
		#print x,y
		pass

	if y % 2 ==1 and x % 2 ==1:
            try:
                (a1,a2,a3)= f.getpixel((x-1,y))
                (b1,b2,b3)= f.getpixel((x+1,y))
                (c1,c2,c3)= f.getpixel((x,y-1))
                (d1,d2,d3)= f.getpixel((x,y+1))

		z1 = int((a1+b1+c1+d1)/4.0)
		z2 = int((a2+b2+c2+d2)/4.0)
		z3 = int((a3+b3+c3+d3)/4.0)
		#print z1,z2,z3
		try:
                    f.putpixel((x,y),(z1,z2,z3))
		except:
                    print z1,z2,z3,x,y
            except:
		#print x,y
		pass



f.save('bbb.jpg')
f.close()

#image = Image.open("cave.jpg")
#nsize = tuple([x / 2 for x in image.size])
#odd = even = Image.new(image.mode, nsize)
#
#for x in range(image.size[0]):
#    for y in range(image.size[1]):
#        if x % 2 == 0 and y % 2 == 0:
#            even.putpixel((x / 2, y / 2), image.getpixel((x, y)))
#        elif x % 2 == 0 and y % 2 == 1:
#            odd.putpixel((x / 2, (y - 1) / 2), image.getpixel((x, y)))
#        elif x % 2 == 1 and y % 2 == 0:
#            even.putpixel(((x - 1) / 2, y / 2), image.getpixel((x, y)))
#        else:
#            odd.putpixel(((x - 1) / 2, (y - 1) / 2), image.getpixel((x, y)))
#
#even.save('even.jpg')
#odd.save('odd.jpg')


f = Image.open("cave.jpg")

#print f.getpixel((0,0))
#f.putpixel((0,0),(255,0,0))
#print f.getpixel((0,0))
#halfsize = tuple([n/2 for n in f.size])

halfsize =(800,800)
#odd = Image.new(f.mode,halfsize)
even = Image.new(f.mode,halfsize)


for x in xrange(f.size[0]):
    for y in xrange(f.size[1]):
        if x % 2 ==0 and y %2 == 0:
            even.putpixel((x/2,y/2),f.getpixel((x,y)))
            #f.putpixel((x,y),(255,0,0))

#odd.save('odd.jpg')
even.save('even.jpg')
#f.save('ttt.jpg') 

import urllib2
import base64
import Image

url="http://www.pythonchallenge.com/pc/return/cave.jpg"

auth="<PASSWORD>"
password=base64.b64encode(auth)
tt="Basic "+password
req = urllib2.Request(url)
req.add_header('Authorization',tt)
data = urllib2.urlopen(req).read()

f = open('cave.jpg','wb')
f.write(data)
f.close()

f = Image.open('cave.jpg')
(h,w) = f.size

print h,w
for x in xrange(int(h)):
    for y in xrange(int(w)):
	if y % 2 ==0 and x % 2 ==0:
            try:
                (a1,a2,a3)= f.getpixel((x-1,y))
                (b1,b2,b3)= f.getpixel((x+1,y))
                (c1,c2,c3)= f.getpixel((x,y-1))
                (d1,d2,d3)= f.getpixel((x,y+1))

		z1 = int((a1+b1+c1+d1)/4.0)
		z2 = int((a2+b2+c2+d2)/4.0)
		z3 = int((a3+b3+c3+d3)/4.0)
		#print z1,z2,z3
		try:
                    f.putpixel((x,y),(z1,z2,z3))
		except:
                    print z1,z2,z3,x,y
            except:
		#print x,y
		pass

	if y % 2 ==1 and x % 2 ==1:
            try:
                (a1,a2,a3)= f.getpixel((x-1,y))
                (b1,b2,b3)= f.getpixel((x+1,y))
                (c1,c2,c3)= f.getpixel((x,y-1))
                (d1,d2,d3)= f.getpixel((x,y+1))

		z1 = int((a1+b1+c1+d1)/4.0)
		z2 = int((a2+b2+c2+d2)/4.0)
		z3 = int((a3+b3+c3+d3)/4.0)
		#print z1,z2,z3
		try:
                    f.putpixel((x,y),(z1,z2,z3))
		except:
                    print z1,z2,z3,x,y
            except:
		#print x,y
		pass



f.save('bbb.jpg')
f.close()

#image = Image.open("cave.jpg")
#nsize = tuple([x / 2 for x in image.size])
#odd = even = Image.new(image.mode, nsize)
#
#for x in range(image.size[0]):
#    for y in range(image.size[1]):
#        if x % 2 == 0 and y % 2 == 0:
#            even.putpixel((x / 2, y / 2), image.getpixel((x, y)))
#        elif x % 2 == 0 and y % 2 == 1:
#            odd.putpixel((x / 2, (y - 1) / 2), image.getpixel((x, y)))
#        elif x % 2 == 1 and y % 2 == 0:
#            even.putpixel(((x - 1) / 2, y / 2), image.getpixel((x, y)))
#        else:
#            odd.putpixel(((x - 1) / 2, (y - 1) / 2), image.getpixel((x, y)))
#
#even.save('even.jpg')
#odd.save('odd.jpg')


f = Image.open("cave.jpg")

#print f.getpixel((0,0))
#f.putpixel((0,0),(255,0,0))
#print f.getpixel((0,0))
#halfsize = tuple([n/2 for n in f.size])

halfsize =(800,800)
#odd = Image.new(f.mode,halfsize)
even = Image.new(f.mode,halfsize)


for x in xrange(f.size[0]):
    for y in xrange(f.size[1]):
        if x % 2 ==0 and y %2 == 0:
            even.putpixel((x/2,y/2),f.getpixel((x,y)))
            #f.putpixel((x,y),(255,0,0))

#odd.save('odd.jpg')
even.save('even.jpg')
#f.save('ttt.jpg') 

#print z1,z2,z3 #print x,y #print z1,z2,z3 #print x,y #image = Image.open("cave.jpg") #nsize = tuple([x / 2 for x in image.size]) #odd = even = Image.new(image.mode, nsize) # #for x in range(image.size[0]): #    for y in range(image.size[1]): #        if x % 2 == 0 and y % 2 == 0: #            even.putpixel((x / 2, y / 2), image.getpixel((x, y))) #        elif x % 2 == 0 and y % 2 == 1: #            odd.putpixel((x / 2, (y - 1) / 2), image.getpixel((x, y))) #        elif x % 2 == 1 and y % 2 == 0: #            even.putpixel(((x - 1) / 2, y / 2), image.getpixel((x, y))) #        else: #            odd.putpixel(((x - 1) / 2, (y - 1) / 2), image.getpixel((x, y))) # #even.save('even.jpg') #odd.save('odd.jpg') #print f.getpixel((0,0)) #f.putpixel((0,0),(255,0,0)) #print f.getpixel((0,0)) #halfsize = tuple([n/2 for n in f.size]) #odd = Image.new(f.mode,halfsize) #f.putpixel((x,y),(255,0,0)) #odd.save('odd.jpg') #f.save('ttt.jpg')