Remove background from images with Python

Today I will show you how you can remove background from your images.

I’ll use numpy, cv2, matplotlib,

NumPy is the fundamental package for scientific computing in Python. It is a Python library that provides a multidimensional array object, var…


This content originally appeared on DEV Community and was authored by Stokry

Today I will show you how you can remove background from your images.

I'll use numpy, cv2, matplotlib,

NumPy is the fundamental package for scientific computing in Python. It is a Python library that provides a multidimensional array object, various derived objects (such as masked arrays and matrices), and an assortment of routines for fast operations on arrays, including mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms, basic linear algebra, basic statistical operations, random simulation and much more.

OpenCV-Python is a library of Python bindings designed to solve computer vision problems.

cv2.imread() method loads an image from the specified file. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format) then this method returns an empty matrix.

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python.

This is our test image:

enter image description here
Let's jump to the code


import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
import sys
from  PIL  import Image

img = cv.imread('images/test.jpg', cv.IMREAD_UNCHANGED)
original = img.copy()

l = int(max(5, 6))
u = int(min(6, 6))

ed = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
edges = cv.GaussianBlur(img, (21, 51), 3)
edges = cv.cvtColor(edges, cv.COLOR_BGR2GRAY)
edges = cv.Canny(edges, l, u)

_, thresh = cv.threshold(edges, 0, 255, cv.THRESH_BINARY  + cv.THRESH_OTSU)
kernel = cv.getStructuringElement(cv.MORPH_ELLIPSE, (5, 5))
mask = cv.morphologyEx(thresh, cv.MORPH_CLOSE, kernel, iterations=4)

data = mask.tolist()
sys.setrecursionlimit(10**8)
for i in  range(len(data)):
    for j in  range(len(data[i])):
        if data[i][j] !=  255:
            data[i][j] =  -1
        else:
            break
    for j in  range(len(data[i])-1, -1, -1):
        if data[i][j] !=  255:
            data[i][j] =  -1
        else:
            break
image = np.array(data)
image[image !=  -1] =  255
image[image ==  -1] =  0

mask = np.array(image, np.uint8)

result = cv.bitwise_and(original, original, mask=mask)
result[mask ==  0] =  255
cv.imwrite('bg.png', result)

img = Image.open('bg.png')
img.convert("RGBA")
datas = img.getdata()

newData = []
for item in datas:
    if item[0] ==  255  and item[1] ==  255  and item[2] ==  255:
        newData.append((255, 255, 255, 0))
    else:
        newData.append(item)

img.putdata(newData)
img.save("img.png", "PNG")

This our result, it's not perfect, but the code did a good job.

enter image description here

That's all. Keep coding.


This content originally appeared on DEV Community and was authored by Stokry


Print Share Comment Cite Upload Translate Updates
APA

Stokry | Sciencx (2021-04-09T08:22:43+00:00) Remove background from images with Python. Retrieved from https://www.scien.cx/2021/04/09/remove-background-from-images-with-python/

MLA
" » Remove background from images with Python." Stokry | Sciencx - Friday April 9, 2021, https://www.scien.cx/2021/04/09/remove-background-from-images-with-python/
HARVARD
Stokry | Sciencx Friday April 9, 2021 » Remove background from images with Python., viewed ,<https://www.scien.cx/2021/04/09/remove-background-from-images-with-python/>
VANCOUVER
Stokry | Sciencx - » Remove background from images with Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/09/remove-background-from-images-with-python/
CHICAGO
" » Remove background from images with Python." Stokry | Sciencx - Accessed . https://www.scien.cx/2021/04/09/remove-background-from-images-with-python/
IEEE
" » Remove background from images with Python." Stokry | Sciencx [Online]. Available: https://www.scien.cx/2021/04/09/remove-background-from-images-with-python/. [Accessed: ]
rf:citation
» Remove background from images with Python | Stokry | Sciencx | https://www.scien.cx/2021/04/09/remove-background-from-images-with-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.