Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions AI Background Remover/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# 🖼️ AI Background Remover

An AI-powered desktop tool that automatically removes image backgrounds and saves the subject as a transparent PNG — perfect for designers, students, and automation scripts.

✅ Built with Python
✅ Uses `rembg` + U²-Net AI model
✅ Works with JPG & PNG images
✅ GUI + Command Line Interface
✅ Hacktoberfest friendly 🎉

---

## 🚀 Features

| Feature | Description |
| ------------------------ | ----------------------------------------- |
| ✂️ AI Background Removal | Removes background using machine learning |
| 🖥️ Streamlit GUI | Upload → Preview → Remove BG → Download |
| 💻 CLI Tool | Command-line support for automation |
| 📦 Output | Saves transparent PNG |
| 🔰 Beginner Friendly | Simple folder structure + clean code |

---

## 📌 Project Structure

```
AI-Background-Remover/
├── src/
│ ├── streamlit_app.py # Streamlit UI version
│ ├── cli_app.py # Command-line background remover
├── assets/
│ ├── sample1.png
│ ├── sample2.png
├── requirements.txt
└── README.md
```

---

## 📥 Installation

### 1️⃣ Clone the Repository

```bash
git clone https://github.com/RK1905101/Mini_Python_Projects/AI-Background-Remover
cd AI-Background-Remover
```

### 2️⃣ Install Dependencies

> Recommended: Use a virtual environment ✅

```bash
pip install -r requirements.txt
```

If `onnxruntime` or `rembg` fails on your system:

```bash
pip install rembg onnxruntime
```

---

## 🖼️ Streamlit GUI Usage

Run the application:

```bash
streamlit run src/streamlit_app.py
```

Then open the browser link shown in the terminal ✅
Upload your image → Click **Remove Background** → Download PNG

---

## 💻 CLI Tool Usage

```bash
python src/cli_app.py input.jpg -o output.png
```

## 📸 Screenshots

| Before |
| ----------------------------------- |
| <img width="200" height="1024" alt="bgremover" src="https://github.com/user-attachments/assets/5b93d1ae-e3cc-424c-a5e6-c5a3bbc75e69" />
| After |
<img width="200" height="1024" alt="output2" src="https://github.com/user-attachments/assets/7087cf33-b6eb-444d-81b2-187a498d4a1d" />|
Comment on lines +90 to +94
Copy link

Copilot AI Oct 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The markdown table structure is malformed. Line 92 has an extra pipe character at the start, and line 94's image tag is outside the table cell structure. This will render incorrectly. The table should have consistent pipe placement: | Before | <img...> | and | After | <img...> |

Suggested change
| Before |
| ----------------------------------- |
| <img width="200" height="1024" alt="bgremover" src="https://github.com/user-attachments/assets/5b93d1ae-e3cc-424c-a5e6-c5a3bbc75e69" />
| After |
<img width="200" height="1024" alt="output2" src="https://github.com/user-attachments/assets/7087cf33-b6eb-444d-81b2-187a498d4a1d" />|
| Before | After |
| ------ | ----- |
| <img width="200" height="1024" alt="bgremover" src="https://github.com/user-attachments/assets/5b93d1ae-e3cc-424c-a5e6-c5a3bbc75e69" /> | <img width="200" height="1024" alt="output2" src="https://github.com/user-attachments/assets/7087cf33-b6eb-444d-81b2-187a498d4a1d" /> |

Copilot uses AI. Check for mistakes.

Binary file added AI Background Remover/assets/bgremover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added AI Background Remover/assets/output2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added AI Background Remover/assets/sample1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added AI Background Remover/assets/sample2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions AI Background Remover/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Requirements for AI Background Remover
# Core packages
rembg>=2.0.0
streamlit>=1.0
Pillow>=9.0

# Recommended runtime for performance (optional)
onnxruntime>=1.15.0 # replace with onnxruntime-gpu for GPU support

# Notes:
# - On Windows you may need Microsoft Visual C++ Build Tools for some packages.
# - If you want GPU acceleration, install `onnxruntime-gpu` instead of `onnxruntime`.
16 changes: 16 additions & 0 deletions AI Background Remover/src/cli_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import argparse
from rembg import remove

def remove_background(input_image, output_image="output.png"):
with open(input_image, "rb") as i:
with open(output_image, "wb") as o:
o.write(remove(i.read()))
Comment on lines +4 to +7
Copy link

Copilot AI Oct 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing error handling for file operations. If the input file doesn't exist or isn't readable, or if the output path is invalid, the function will crash with an unhelpful error. Consider adding try-except blocks to catch FileNotFoundError and PermissionError, providing clear error messages to users.

Copilot uses AI. Check for mistakes.
print(f"✅ Background removed! Saved as {output_image}")

if __name__ == "__main__":
parser = argparse.ArgumentParser(description="AI Background Remover CLI Tool")
parser.add_argument("input", help="Input image path")
parser.add_argument("-o", "--output", default="output.png", help="Output image path")
args = parser.parse_args()

remove_background(args.input, args.output)
41 changes: 41 additions & 0 deletions AI Background Remover/src/streamlit_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import streamlit as st
from rembg import remove
from PIL import Image
import io

st.set_page_config(page_title="AI Background Remover", page_icon="🖼️", layout="centered")
st.title("🖼️ AI Background Remover")

uploaded_file = st.file_uploader("Upload an image (PNG/JPG)", type=["png", "jpg", "jpeg"])

if uploaded_file:

# Display original image
img = Image.open(uploaded_file)
st.subheader("Original Image")
st.image(img, use_container_width=True)

if st.button("Remove Background"):
with st.spinner("Processing... Please wait"):

uploaded_file.seek(0) # ✅ Reset pointer before second read
input_image = uploaded_file.read()

output = remove(input_image)
result_image = Image.open(io.BytesIO(output))

st.success("✅ Background Removed Successfully!")
st.subheader("Output Image")
st.image(result_image, use_container_width=True)

# Download button
buf = io.BytesIO()
result_image.save(buf, format="PNG")
byte_data = buf.getvalue()

st.download_button(
label="📥 Download Output",
data=byte_data,
file_name="removed_bg.png",
mime="image/png"
)