Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing borders from an image in Python

Tags:

python

opencv

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
import Image, scipy

filename = "snap.jpeg"

img = cv2.imread(filename)

def checkEqual(lst):
    return len(set(lst)) <= 1 ## <-- This is the maximum length of the set

def removeColumns(image):
    for col in range(image.shape[1]):
        for ch in range(3):
            try:
                checkEqual(image[:, col, ch].tolist())
            except IndexError:
                continue
            else:
                if checkEqual(image[:, col, ch].tolist()):
                    try:
                        image = numpy.delete(image, col, 1)
                    except IndexError:
                        continue
                    else:
                        pass
    return image

img2 = removeColumns(img)

print img.shape, img2.shape ## (480, 856, 3) (480, 705, 3)

Here I find the columns that have the same elements and all the videos that I have have black borders. But even if I increase the maximum length in the function checkEqual() from 1 to 20 or 40, the whole black strip is not deleted.

This is the original image: enter image description here

This is the image after running the program: enter image description here

Could anyone give suggestion for a better solution to this problem ? Thanks!

like image 665
Animesh Pandey Avatar asked Oct 09 '13 12:10

Animesh Pandey


People also ask

How do you remove a border from a picture?

Remove a border from a picture Select the picture whose border you want to remove. On the Page Layout tab, in the Page Background group, select Page Borders. Click the Borders tab. Under Setting, select None.

How do I remove the border from an image in CSS?

Adding border="0" to your img tag prevents that picture from having a border around the image. However, adding border="0" to every image would not only be time consuming but also increase the file size and download time. To prevent all images from having a border, create a CSS rule or file with the following code.

How do you add a border in Python?

Create a frame with Frame() method. Highlight the border of the frame with a color, highlightbackground="blue". Then, set the thickness of the border, highlightthickness=2. Next, create some widgets inside the frame.


2 Answers

This problem was already solved in this answer.

from PIL import Image, ImageChops

im = Image.open('iI3ZE.jpg')

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)


trim(im).show()

I used Pillow instead of PIL:

pip install pillow

Results in:

enter image description here

like image 52
metakermit Avatar answered Nov 15 '22 12:11

metakermit


I think that you will find that the problem goes away if you work from the other side of the image, as you are checking the first column (col[0] - it's black so you delete it and the black col[1] becomes col[0] then you check the col[1] - skipping the new col[0]....

If you start from the max it would work or if you stay on any given col once you have deleted it. Alternatively you can make a list of which to delete, reverse it and then do the deletions.

like image 25
Steve Barnes Avatar answered Nov 15 '22 13:11

Steve Barnes