DEV Community

Pablito Piova
Pablito Piova

Posted on

Azure AI Vision - Remove backgrounds from images



This article demonstrates how to use the Image Analysis 4.0 API to segment an image by removing its background. You will learn how to authenticate with the service and how to use both the REST API and the client SDK to accomplish this task.

Before starting you can test this feature of the service with a NO CODE experience in Azure AI | Vision Studio

🎞️Background Removal in Vision Studio


Before proceeding with this guide, please review my previous post where I explained this Azure AI Vision service Post Azure AI Vision and then make sure you have followed the steps outlined in the Quickstart page. This means:

  • You have gained an understanding of this Azure AI service.
  • You have obtained an API key and endpoint URL.
  • If you're using the client SDK, you have the appropriate SDK package installed and a running quickstart application.
  • If you're making direct 4.0 REST API calls, you have successfully made a curl.exe call to the service (or used an alternative tool).

Important: Background removal is only available in certain Azure regions such as East US, France Central, Korea Central, North Europe, Southeast Asia, West Europe, and West US.



from import ComputerVisionClient
from msrest.authentication import CognitiveServicesCredentials

subscription_key = "YOUR_SUBSCRIPTION_KEY"
endpoint = "YOUR_ENDPOINT_URL"

client = ComputerVisionClient(endpoint, CognitiveServicesCredentials(subscription_key))
Enter fullscreen mode Exit fullscreen mode


using Microsoft.Azure.CognitiveServices.Vision.ComputerVision;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;

string subscriptionKey = "YOUR_SUBSCRIPTION_KEY";
string endpoint = "YOUR_ENDPOINT_URL";

ComputerVisionClient client = new ComputerVisionClient(new ApiKeyServiceClientCredentials(subscriptionKey))
    Endpoint = endpoint
Enter fullscreen mode Exit fullscreen mode


curl -X POST "YOUR_ENDPOINT_URL/vision/v4.0/analyze?visualFeatures=BackgroundRemoval" \
     -H "Ocp-Apim-Subscription-Key: YOUR_SUBSCRIPTION_KEY" \
     -H "Content-Type: application/json" \
     -d "{'url':'IMAGE_URL'}"
Enter fullscreen mode Exit fullscreen mode

Using the Client SDK


from import VisualFeatureTypes

image_url = "YOUR_IMAGE_URL";

analysis = client.analyze_image_by_domain("BackgroundRemoval", image_url)
result = analysis.result
Enter fullscreen mode Exit fullscreen mode


string imageUrl = "YOUR_IMAGE_URL";
var features = new List<VisualFeatureTypes?>() { VisualFeatureTypes.BackgroundRemoval };

var result = await client.AnalyzeImageAsync(imageUrl, features);

// The 'result' object will contain the image with the background removed
Enter fullscreen mode Exit fullscreen mode


  • Maximum image size: 4 MB
  • Supported image formats: JPEG, PNG, GIF, BMP
  • API call limit: Varies depending on the subscription plan


Console App in .NET

I share the repository on GitHub Here

Gradio Web App in Python

I share the repository on GitHub Here


Background removal is a powerful tool in image processing, especially useful in applications like e-commerce, photography, and more. With Azure's Image Analysis 4.0 API, performing this task is now easier and more accessible than ever.

I hope this explanation has been greatly helpful! Feel free to leave your comments and questions.
👋Until next time, community

Top comments (0)