def pw_affine(fromim, toim, fp, tp, tri):
 im = toim.copy()
 is_color = (len(fromim.shape) == 3)
 im_t = zeros(im.shape, 'uint8')
 for t in tri:
  H = homography.Haffine_from_points(tp[:, t], fp[:, t])
  if is_color:
   for col in range(fromim.shape[2]):
    im_t[:, :, col] = ndimage.affine_transform(fromim[:, :, col], H[:2, :2], (H[(0, 2)], H[(1, 2)]), im.shape[:2])
  else:
   im_t = ndimage.affine_transform(fromim, H[:2, :2], (H[(0, 2)], H[(1, 2)]), im.shape[:2])
  alpha = alpha_for_triangle(tp[:, t], im.shape[0], im.shape[1])
  im[(alpha > 0)] = im_t[(alpha > 0)]
 return im

def pw_affine ( fromim , toim , fp , tp , tri ) : im = toim . copy ( ) is_color = ( len ( fromim . shape ) == 3 ) im_t = zeros ( im . shape , 'uint8' ) for t in tri : H = homography . Haffine_from_points ( tp [ : , t ] , fp [ : , t ] ) if is_color : for col in range ( fromim . shape [ 2 ] ) : im_t [ : , : , col ] = ndimage . affine_transform ( fromim [ : , : , col ] , H [ : 2 , : 2 ] , ( H [ ( 0 , 2 ) ] , H [ ( 1 , 2 ) ] ) , im . shape [ : 2 ] ) else : im_t = ndimage . affine_transform ( fromim , H [ : 2 , : 2 ] , ( H [ ( 0 , 2 ) ] , H [ ( 1 , 2 ) ] ) , im . shape [ : 2 ] ) alpha = alpha_for_triangle ( tp [ : , t ] , im . shape [ 0 ] , im . shape [ 1 ] ) im [ ( alpha > 0 ) ] = im_t [ ( alpha > 0 ) ] return im
warp triangular patches from an image .