GithubHelp home page GithubHelp logo

Comments (3)

Striker2k avatar Striker2k commented on May 23, 2024

Code :

import rembg
import cv2
from PIL import Image

def remove_background(image_path, output_path):
input_image = cv2.imread(image_path)
input_image_rgb = cv2.cvtColor(input_image, cv2.COLOR_BGR2RGB)
output_image = rembg. remove(input_image_rgb)
output_image_gray = cv2.cvtColor(output_image, cv2.COLOR_RGB2GRAY"
contours, _= cv2.findContours(output_image_gray, cv2.RETR_EXTERNAL, cV2.CHAIN_APPROX_SIMPLE)

if len(contours) > 0:
    largest_contour = max(contours, key=cv2.contourArea)
    x, y, w, h = cv2.boundingRect(largest_contour)
    x1, y1, x2, y2 = x, y, x+w, y+h
    cropped_output_image = output_image[y1:y2, x1:x2]
    cropped_output_image_pil = Image.fromarray(cropped_output_image)

    cropped_output_image_pil.save(output_path)
    return output_path
else:
    print("Error: No foreground detected.")
    return None

image_path = "Media.jpeg"
output_path = "error.png"
remove_background(image_path, output_path)

Error.png / output 20240402_125300.jpg

from rembg.

github-actions avatar github-actions commented on May 23, 2024

This issue is stale because it has been open for 30 days with no activity.

from rembg.

github-actions avatar github-actions commented on May 23, 2024

This issue was closed because it has been inactive for 14 days since being marked as stale.

from rembg.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.