def reconstruct_from_patches_2d(patches, image_size):
 (i_h, i_w) = image_size[:2]
 (p_h, p_w) = patches.shape[1:3]
 img = np.zeros(image_size)
 n_h = ((i_h - p_h) + 1)
 n_w = ((i_w - p_w) + 1)
 for (p, (i, j)) in zip(patches, product(range(n_h), range(n_w))):
  img[i:(i + p_h), j:(j + p_w)] += p
 for i in range(i_h):
  for j in range(i_w):
   img[(i, j)] /= float((min((i + 1), p_h, (i_h - i)) * min((j + 1), p_w, (i_w - j))))
 return img

def reconstruct_from_patches_2d ( patches , image_size ) : ( i_h , i_w ) = image_size [ : 2 ] ( p_h , p_w ) = patches . shape [ 1 : 3 ] img = np . zeros ( image_size ) n_h = ( ( i_h - p_h ) + 1 ) n_w = ( ( i_w - p_w ) + 1 ) for ( p , ( i , j ) ) in zip ( patches , product ( range ( n_h ) , range ( n_w ) ) ) : img [ i : ( i + p_h ) , j : ( j + p_w ) ] += p for i in range ( i_h ) : for j in range ( i_w ) : img [ ( i , j ) ] /= float ( ( min ( ( i + 1 ) , p_h , ( i_h - i ) ) * min ( ( j + 1 ) , p_w , ( i_w - j ) ) ) ) return img
reconstruct the image from all of its patches .