<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def trim_extrema(im, h, mode='maxima'):
 r""" Trims local extrema in greyscale values by a specified amount. This essentially decapitates peaks and/or floods valleys. Parameters im : ND-array The image whose extrema are to be removed h : float The height to remove from each peak or fill in each valley mode : string {'maxima' | 'minima' | 'extrema'} Specifies whether to remove maxima or minima or both Returns ------- image : ND-array A copy of the input image with all the peaks and/or valleys removed. Notes ----- This function is referred to as **imhmax** or **imhmin** in Matlab. """

    result = im
    if mode in ['maxima', 'extrema']:
        result = reconstruction(seed=im - h, mask=im, method='dilation')
    elif mode in ['minima', 'extrema']:
        result = reconstruction(seed=im + h, mask=im, method='erosion')
    return result