0
点赞
收藏
分享

微信扫一扫

python 去掉图片中四边的白色/浅色

MaxWen 2024-10-10 阅读 13


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")


举报

相关推荐

0 条评论