Removing borders from an image in Python

前端 未结 4 992
孤城傲影
孤城傲影 2021-01-13 02:55

Some videos have frames that have black strips like borders. I have to remove them from the frames. I came up with a crude solution:

import sys, cv2, numpy
i         


        
4条回答
  •  旧巷少年郎
    2021-01-13 03:37

    This problem was already solved in this answer.

    In [1]: from PIL import Image, ImageChops
    
    In [3]: im = Image.open('iI3ZE.jpg')
    
    In [4]: def trim(im):
       ...:         bg = Image.new(im.mode, im.size, im.getpixel((0,0)))
       ...:         diff = ImageChops.difference(im, bg)
       ...:         diff = ImageChops.add(diff, diff, 2.0, -100)
       ...:         bbox = diff.getbbox()
       ...:         if bbox:
       ...:                 return im.crop(bbox)
       ...:
    
    In [5]: trim(im).show()
    

    I used Pillow instead of PIL:

    pip install pillow
    

    Results in:

    enter image description here

提交回复
热议问题