#!/usr/bin/python

import sys
from itertools import *

def grouper(iterable, n, fillvalue=None):
    args = [iter(iterable)] * n
    return izip_longest(*args, fillvalue=fillvalue)

def rgb_to_bin(r,g,b):
    return int(hex(r)[2:] + hex(g)[2:] + hex(b)[2:], 16)

def bin_to_rgb(b):
    h = '%06X'%(b)
    return map(lambda x:int(''.join(x), 16), grouper(h, 2))

def pnm_to_bin(p):
    w,h = map(int,p[1].split(' '))
    data = map(int, ' '.join(p[3:]).replace('\n', ' ').split())
    bin = []
    lines = grouper(data, w*3)
    for line in lines:
        data = []
        for rgb in grouper(line, 3):
            data.append(rgb_to_bin(*rgb))
        bin.append(data)
    return bin

def bin_to_pnm(b):
    pnm = 'P3 {} {} 255 '.format(len(b[0]), len(b))
    b = [z for a in b for z in a]
    pnm += ' '.join(map(lambda x:' '.join(map(str, bin_to_rgb(x))), b))
    return pnm

def imageblender(img):
    h = len(img)
    w = len(img[0])
    for y in range(w):
        for x in range(h):
            val = (img[x][y] + img[x][~y] + img[~x][y] + img[~x][~y])//4
            img[x][y],img[x][~y],img[~x][y],img[~x][~y] = (val,)*4
    return img

def main(fname):
    bin = pnm_to_bin(open(fname).read().split('\n'))
    bin = imageblender(bin)
    return bin_to_pnm(bin)

if __name__ == '__main__':
    print main(sys.argv[1])

@@ -140,16 +140,21 @@
  return 
+list(
 izip_lon
@@ -185,24 +185,25 @@
 llvalue)
+)
 %0D%0A%0D%0Adef 
 rgb_to_b
@@ -198,186 +198,103 @@
 def 
-rgb_to_bin(r,g,b):%0D%0A    return int(hex(r)%5B2:%5D + hex(g)%5B2:%5D + hex(b)%5B2:%5D, 16)%0D%0A%0D%0Adef bin_to_rgb(b):%0D%0A    h = '%2506X'%25(b)%0D%0A    return map(lambda x:int(''.join(x), 16), grouper(h, 2)
+flatten(lst):%0D%0A    return sum((%5Bx%5D if not isinstance(x, list) else flatten(x) for x in lst), %5B%5D
 )%0D%0A%0D
@@ -570,20 +570,13 @@
 end(
-rgb_to_bin(*
+list(
 rgb)
@@ -707,33 +707,18 @@
 b = 
-%5Bz for a in b for z in a%5D
+flatten(b)
 %0D%0A  
@@ -743,50 +743,11 @@
 map(
-lambda x:' '.join(map(str, bin_to_rgb(x)))
+str
 , b)
@@ -878,24 +878,60 @@
  range(h):%0D%0A
+            for i in range(3):%0D%0A    
             
@@ -942,24 +942,27 @@
 = (img%5Bx%5D%5By%5D
+%5Bi%5D
  + img%5Bx%5D%5B~y
@@ -962,16 +962,19 @@
 g%5Bx%5D%5B~y%5D
+%5Bi%5D
  + img%5B~
@@ -978,16 +978,19 @@
 g%5B~x%5D%5By%5D
+%5Bi%5D
  + img%5B~
@@ -995,16 +995,19 @@
 %5B~x%5D%5B~y%5D
+%5Bi%5D
 )//4%0D%0A  
@@ -1016,16 +1016,20 @@
         
+    
 img%5Bx%5D%5By
@@ -1025,24 +1025,27 @@
    img%5Bx%5D%5By%5D
+%5Bi%5D
 ,img%5Bx%5D%5B~y%5D,
@@ -1043,16 +1043,19 @@
 g%5Bx%5D%5B~y%5D
+%5Bi%5D
 ,img%5B~x%5D
@@ -1057,16 +1057,19 @@
 g%5B~x%5D%5By%5D
+%5Bi%5D
 ,img%5B~x%5D
@@ -1072,16 +1072,19 @@
 %5B~x%5D%5B~y%5D
+%5Bi%5D
  = (val,

fix bug