These 3 lines can be written more compact, for example:

    if mysize.shape == ():
        np.repeat(mysize.item(), im.ndim)