Remove borders from image using Python OpenCV

I have images with Borders like the below. Can I use OpenCV or python to remove the borders like this in images?

I used the following code to crop, but it didn’t work.

copy = Image.fromarray(img_final_bin)
try:
    bg = Image.new(copy.mode, copy.size, copy.getpixel((0, 0)))
except:
    return None
diff = ImageChops.difference(copy, bg)
diff = ImageChops.add(diff, diff, 2.0, -100)
bbox = diff.getbbox()
if bbox:
    return np.array(copy.crop(bbox))

Image 1

Image 2

%d bloggers like this: