0

How to remove background from an image programmatically in flutter. I want to remove background from this below image .enter image description here

Ranjith Jith
  • 21
  • 1
  • 5

3 Answers3

1

Use Remove Bg API.

official website Remove bg

For More Information Remove API

Jeel Bhatti
  • 651
  • 3
  • 15
  • Hi , Thankyou for your response. Remove BG API available only native android(JAVA) and Ios(Swift). I need to using Flutter Project. – Ranjith Jith Dec 09 '21 at 09:04
  • This is available using http package. And they provide rest API that's why we can use in flutter. Review this https://stackoverflow.com/questions/60626619/is-it-possible-to-develop-an-app-using-the-remove-bg-api-in-flutter. – Jeel Bhatti Dec 09 '21 at 09:34
  • removebg API response get only image . how to set this image in my flutter project. – Ranjith Jith Dec 09 '21 at 12:19
  • @PrajapatiAmit Are you time traveler? – Ranjith Jith May 05 '22 at 12:21
0

pass your image path to this code:
assets/imagepath.jpg : rename this with your path,

https://www.remove.bg/dashboard#api-key[][1] open this link and get the api key and copy,

import 'dart:typed_data';
import 'package:http/http.dart' as http;

class ApiClient {
  Future<Uint8List> removeBgApi(String imagePath) async {
    var request = http.MultipartRequest("POST", Uri.parse("https://api.remove.bg/v1.0/removebg"));
    request.files.add(await http.MultipartFile.fromPath("image_file", imagePath));
    request.headers.addAll({"X-API-Key": " replace with your api key "});
    final response = await request.send();
    if (response.statusCode == 200) {
      http.Response imgRes = await http.Response.fromStream(response);
      return imgRes.bodyBytes;
    } else {
      throw Exception("Error occurred with response ${response.statusCode}");
    }
  }
}



ApiClient().removeBgApi('assets/imagepath.jpg');
-2

Using http package, you can access remove.bg rest api & get removed background image.

To remove.bg rest api, you can pass/input image using multipart request & from the response, you can get streamed response using http.Response.fromStream() to get removed background image. Below code snippet for the reference,

var request = new http.MultipartRequest("POST", remove_bg_api_uri);
var streamedResponse = await request.send()
var response = await http.Response.fromStream(streamedResponse);

For know more details on how to remove image background using remove.bg api, refer this link.

ritsapp
  • 80
  • 2