import numpy as np def clean_bounds(xl, yl, xu, yu, shape): if xl < 0: xl = 0 if yl < 0: yl = 0 if xu > shape[0]: xu = shape[0] if yu > shape[1]: yu = shape[1] return xl, yl, xu, yu def clean_bounds_offset(x: int, y: int, offset: int, shape: tuple[int, int]): xl = x - offset xu = x + offset + 1 yl = y - offset yu = y + offset + 1 # if isinstance(x, np.ndarray): # xl[xl < 0] = 0 # xu[xu > shape[0]] = shape[0] # xl = xl.astype(np.int64) # xu = xu.astype(np.int64) # else: if xl < 0: xl = 0 if xu > shape[0]: xu = shape[0] # if isinstance(y, np.ndarray): # yl[yl < 0] = 0 # yu[yu > shape[1]] = shape[1] # yl = yl.astype(np.int64) # yu = yu.astype(np.int64) # else: if yl < 0: yl = 0 if yu > shape[1]: yu = shape[1] return xl, yl, xu, yu