from PIL import Image
image = Image.open("input_image.jpg")
left = image.width
top = image.height
right = 0
bottom = 0
pixels = image.load()
threshold = 200
for y in range(image.height):
for x in range(image.width):
rgb = pixels[x, y]
if not (rgb[0] >= threshold and rgb[1] >= threshold and rgb[2] >= threshold):
left = min(left, x)
top = min(top, y)
right = max(right, x)
bottom = max(bottom, y)
cropped_image = image.crop((left, top, right + 1, bottom + 1))
cropped_image.save("output_image.jpg")