Skip to content

Commit 747334b

Browse files
authored
Merge pull request #1 from Netesh5/feat/add_bg
Feat/add bg
2 parents bc651dc + abe6dbf commit 747334b

File tree

8 files changed

+155
-9
lines changed

8 files changed

+155
-9
lines changed

.flutter-plugins

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# This is a generated file; do not edit or check into version control.
2+
onnxruntime=/Users/neteshpaudel/.pub-cache/hosted/pub.dev/onnxruntime-1.4.1/

.flutter-plugins-dependencies

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"onnxruntime","path":"/Users/neteshpaudel/.pub-cache/hosted/pub.dev/onnxruntime-1.4.1/","native_build":true,"dependencies":[]}],"android":[{"name":"onnxruntime","path":"/Users/neteshpaudel/.pub-cache/hosted/pub.dev/onnxruntime-1.4.1/","native_build":true,"dependencies":[]}],"macos":[{"name":"onnxruntime","path":"/Users/neteshpaudel/.pub-cache/hosted/pub.dev/onnxruntime-1.4.1/","native_build":true,"dependencies":[]}],"linux":[{"name":"onnxruntime","path":"/Users/neteshpaudel/.pub-cache/hosted/pub.dev/onnxruntime-1.4.1/","native_build":true,"dependencies":[]}],"windows":[{"name":"onnxruntime","path":"/Users/neteshpaudel/.pub-cache/hosted/pub.dev/onnxruntime-1.4.1/","native_build":true,"dependencies":[]}],"web":[]},"dependencyGraph":[{"name":"onnxruntime","dependencies":[]}],"date_created":"2025-01-30 15:37:32.599262","version":"3.24.5","swift_package_manager_enabled":false}

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.0.6
2+
3+
### Feat
4+
- Added `addBackground` function to change background of color
5+
16
## 0.0.5
27

38
### Fix

README.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ A Flutter package that removes the background from images using an ONNX model. T
1010
- Works entirely offline, ensuring privacy and reliability.
1111
- Lightweight and optimized for efficient performance.
1212
- Simple and seamless integration with Flutter projects.
13+
- Add a custom background color to images.
1314

1415
---
1516

@@ -31,7 +32,7 @@ dependencies:
3132
## Usage
3233
# Initialization
3334
Before using the `removeBg` method, you must initialize the ONNX environment:
34-
```
35+
```dart
3536
import 'package:image_background_remover/image_background_remover.dart';
3637
3738
@override
@@ -40,11 +41,10 @@ Before using the `removeBg` method, you must initialize the ONNX environment:
4041
BackgroundRemover.instance.initializeOrt();
4142
}
4243
43-
```
4444
4545
# Remove Background
4646
To remove the background from an image:
47-
```
47+
```dart
4848
import 'dart:typed_data';
4949
import 'package:image_background_remover/image_background_remover.dart';
5050

@@ -53,16 +53,32 @@ ui.Image resultImage = await BackgroundRemover.instance.removeBg(imageBytes);
5353
/* resultImage will contain image with transparent background*/
5454

5555

56+
```
57+
58+
## 🆕 New Feature: Add Background Color
59+
60+
You can now add a custom background color to images after removing the background.
61+
62+
### Usage:
63+
64+
```dart
65+
Uint8List modifiedImage = await BackgroundRemover.instance.addBackground(
66+
image: originalImageBytes,
67+
bgColor: Colors.white, // Set your desired background color
68+
);
69+
5670
```
5771

5872
## API
5973

6074
### Methods
6175

62-
| Method | Description | Parameters | Returns |
63-
|-------------------------|-----------------------------------------------------------------------------|---------------------------------|-----------------------------------|
64-
| `initializeOrt()` | Initializes the ONNX runtime environment. Call this method once before using `removeBg`. | None | `Future<void>` |
76+
| Method | Description | Parameters | Returns |
77+
|---------------------------------|-----------------------------------------------------------------------------|------------------------------------------------|-----------------------------------|
78+
| `initializeOrt()` | Initializes the ONNX runtime environment. Call this method once before using `removeBg`. | None | `Future<void>` |
6579
| `removeBg(Uint8List imageBytes)` | Removes the background from an image. | `imageBytes` - The image in byte array format. | `Future<ui.Image>` - The processed image with the background removed. |
80+
| `addBackground({required Uint8List image, required Color bgColor})` | Adds a background color to the given image. | `image` - The original image in byte array format. <br> `bgColor` - The background color to be applied. | `Future<Uint8List>` - The modified image with the background color applied. |
81+
6682

6783

6884
## ⚠️ Warning

example/lib/main.dart

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,31 @@ class _MyHomePageState extends State<MyHomePage> {
9696
return const CircularProgressIndicator();
9797
} else if (snapshot.connectionState ==
9898
ConnectionState.done) {
99-
return Image.memory(snapshot.data!);
99+
return Column(
100+
children: [
101+
Image.memory(snapshot.data!),
102+
FutureBuilder(
103+
future: BackgroundRemover
104+
.instance
105+
.addBackground(
106+
image: snapshot.data!,
107+
bgColor: Colors.orange),
108+
builder: (context, snapshot) {
109+
if (snapshot
110+
.connectionState ==
111+
ConnectionState.waiting) {
112+
return const CircularProgressIndicator();
113+
} else if (snapshot
114+
.connectionState ==
115+
ConnectionState.done) {
116+
return Image.memory(
117+
snapshot.data!);
118+
} else {
119+
return const Text('Error');
120+
}
121+
}),
122+
],
123+
);
100124
} else {
101125
return const Text('Error');
102126
}

example/pubspec.lock

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# Generated by pub
22
# See https://dart.dev/tools/pub/glossary#lockfile
33
packages:
4+
archive:
5+
dependency: transitive
6+
description:
7+
name: archive
8+
sha256: "6199c74e3db4fbfbd04f66d739e72fe11c8a8957d5f219f1f4482dbde6420b5a"
9+
url: "https://pub.dev"
10+
source: hosted
11+
version: "4.0.2"
412
async:
513
dependency: transitive
614
description:
@@ -49,6 +57,14 @@ packages:
4957
url: "https://pub.dev"
5058
source: hosted
5159
version: "0.3.4+2"
60+
crypto:
61+
dependency: transitive
62+
description:
63+
name: crypto
64+
sha256: "1e445881f28f22d6140f181e07737b22f1e099a5e1ff94b0af2f9e4a463f4855"
65+
url: "https://pub.dev"
66+
source: hosted
67+
version: "3.0.6"
5268
cupertino_icons:
5369
dependency: "direct main"
5470
description:
@@ -152,13 +168,21 @@ packages:
152168
url: "https://pub.dev"
153169
source: hosted
154170
version: "4.0.2"
171+
image:
172+
dependency: transitive
173+
description:
174+
name: image
175+
sha256: "8346ad4b5173924b5ddddab782fc7d8a6300178c8b1dc427775405a01701c4a6"
176+
url: "https://pub.dev"
177+
source: hosted
178+
version: "4.5.2"
155179
image_background_remover:
156180
dependency: "direct main"
157181
description:
158182
path: ".."
159183
relative: true
160184
source: path
161-
version: "0.0.2"
185+
version: "0.0.5"
162186
image_picker:
163187
dependency: "direct main"
164188
description:
@@ -303,6 +327,14 @@ packages:
303327
url: "https://pub.dev"
304328
source: hosted
305329
version: "1.9.0"
330+
petitparser:
331+
dependency: transitive
332+
description:
333+
name: petitparser
334+
sha256: c15605cd28af66339f8eb6fbe0e541bfe2d1b72d5825efc6598f3e0a31b9ad27
335+
url: "https://pub.dev"
336+
source: hosted
337+
version: "6.0.2"
306338
plugin_platform_interface:
307339
dependency: transitive
308340
description:
@@ -311,6 +343,14 @@ packages:
311343
url: "https://pub.dev"
312344
source: hosted
313345
version: "2.1.8"
346+
posix:
347+
dependency: transitive
348+
description:
349+
name: posix
350+
sha256: a0117dc2167805aa9125b82eee515cc891819bac2f538c83646d355b16f58b9a
351+
url: "https://pub.dev"
352+
source: hosted
353+
version: "6.0.1"
314354
sky_engine:
315355
dependency: transitive
316356
description: flutter
@@ -396,6 +436,14 @@ packages:
396436
url: "https://pub.dev"
397437
source: hosted
398438
version: "1.1.0"
439+
xml:
440+
dependency: transitive
441+
description:
442+
name: xml
443+
sha256: b015a8ad1c488f66851d762d3090a21c600e479dc75e68328c52774040cf9226
444+
url: "https://pub.dev"
445+
source: hosted
446+
version: "6.5.0"
399447
sdks:
400448
dart: ">=3.5.4 <4.0.0"
401449
flutter: ">=3.24.0"

lib/src/background_remover.dart

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:flutter/material.dart';
77
import 'package:flutter/services.dart';
88
import 'package:image_background_remover/assets.dart';
99
import 'package:onnxruntime/onnxruntime.dart';
10+
import 'package:image/image.dart' as img;
1011

1112
class BackgroundRemover {
1213
BackgroundRemover._internal();
@@ -59,8 +60,20 @@ class BackgroundRemover {
5960

6061
/// Removes the background from an image.
6162
///
63+
/// This function processes the input image and removes its background,
64+
/// returning a new image with the background removed.
65+
///
6266
/// - [imageBytes]: The input image as a byte array.
6367
/// - Returns: A [ui.Image] with the background removed.
68+
///
69+
/// Example usage:
70+
/// ```dart
71+
/// final imageBytes = await File('path_to_image').readAsBytes();
72+
/// final ui.Image imageWithoutBackground = await removeBackground(imageBytes);
73+
/// ```
74+
///
75+
/// Note: This function may take some time to process depending on the size
76+
/// and complexity of the input image.
6477
Future<ui.Image> removeBg(Uint8List imageBytes) async {
6578
if (_session == null) {
6679
throw Exception("ONNX session not initialized");
@@ -149,6 +162,14 @@ class BackgroundRemover {
149162
}
150163

151164
/// Applies the mask to the original image and generates the final output.
165+
///
166+
/// This method takes the mask generated by the background removal algorithm
167+
/// and applies it to the original image to produce the final image with the
168+
/// background removed. The resulting image will have the background pixels
169+
/// replaced with transparency or a specified color.
170+
///
171+
/// Returns:
172+
/// A new image with the background removed.
152173
Future<ui.Image> _applyMaskToOriginalSizeImage(
153174
ui.Image image, List resizedMask) async {
154175
final byteData = await image.toByteData(format: ui.ImageByteFormat.rawRgba);
@@ -179,4 +200,32 @@ class BackgroundRemover {
179200

180201
return completer.future;
181202
}
203+
204+
/// Adds a background color to the given image.
205+
///
206+
/// This method takes an image in the form of a [Uint8List] and a background
207+
/// color as a [Color]. It decodes the image, creates a new image with the
208+
/// same dimensions, fills it with the specified background color, and then
209+
/// composites the original image onto the new image with the background color.
210+
///
211+
/// Returns a [Future] that completes with the modified image as a [Uint8List].
212+
///
213+
/// - Parameters:
214+
/// - image: The original image as a [Uint8List].
215+
/// - bgColor: The background color as a [Color].
216+
///
217+
/// - Returns: A [Future] that completes with the modified image as a [Uint8List].
218+
Future<Uint8List> addBackground(
219+
{required Uint8List image, required Color bgColor}) async {
220+
final img.Image decodedImage = img.decodeImage(image)!;
221+
final newImage =
222+
img.Image(width: decodedImage.width, height: decodedImage.height);
223+
img.fill(newImage,
224+
color: img.ColorRgb8(bgColor.red, bgColor.green, bgColor.blue));
225+
img.compositeImage(newImage, decodedImage);
226+
final jpegBytes = img.encodeJpg(newImage);
227+
final completer = Completer<Uint8List>();
228+
completer.complete(jpegBytes.buffer.asUint8List());
229+
return completer.future;
230+
}
182231
}

pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: image_background_remover
22
description: "A Flutter package that removes the background from images using an ONNX model."
3-
version: 0.0.5
3+
version: 0.0.6
44
homepage: https://github.com/Netesh5/image_background_remover/tree/main
55

66
environment:
@@ -10,6 +10,7 @@ environment:
1010
dependencies:
1111
flutter:
1212
sdk: flutter
13+
image: ^4.5.2
1314
onnxruntime: ^1.4.1
1415

1516
dev_dependencies:

0 commit comments

Comments
 (0)