Removing Backgrounds
We’re going to walk through how to take an input image, create a mask for the background, and use it to remove the background leaving just the foreground.
We’ll be using one of our cute puppies-in-the-clouds gens as our input image:

Once we are finished we’ll have removed the clouds and we be left with just cute puppies.
Project Setup
Section titled “Project Setup”# Create a project directory.mkdir prodia-removing-backgroundscd prodia-removing-backgroundsInstall Node (if not already installed):
brew install node# Close the current terminal and open a new one so that node is available.apt install node# Close the current terminal and open a new one so that node is available.winget install -e --id OpenJS.NodeJS.LTS# Close the current terminal and open a new one so that node is available.Create project skeleton:
# Requires node --version >= 18# Initialize the project with npm.npm init -y
# Install the prodia-js library.npm install prodia --saveInstall Python (if not already installed):
brew install python# Close the current terminal and open a new one so that python is available.apt install python3 python3-venv python-is-python3# Close the current terminal and open a new one so that python is available.winget install -e --id Python.Python.3.12# Close the current terminal and open a new one so that python is available.# Requires python --version >= 3.12python -m venv venvsource venv/bin/activatepip install requestsInstall curl (if not already installed):
brew install curl# Close the current terminal and open a new one so that curl is available.apt install curl# Close the current terminal and open a new one so that curl is available.# NOTE: Windows 10 and up have curl installed by default and this can be# skipped.winget install -e --id cURL.cURL# Close the current terminal and open a new one so that curl is available.# Export your token so it can be used by the main code.export PRODIA_TOKEN=your-token-hereYour token is exported to an environment variable. If you close or switch your
shell you’ll need to run export PRODIA_TOKEN=your-token-here again.
Create a main file for your project:
const { createProdia } = require("prodia/v2");
const prodia = createProdia({    token: process.env.PRODIA_TOKEN // get it from environment});Create the following main.py
from requests.adapters import HTTPAdapter, Retryimport osimport requestsimport sys
prodia_token = os.getenv('PRODIA_TOKEN')prodia_url = 'https://inference.prodia.com/v2/job'
session = requests.Session()retries = Retry(allowed_methods=None, status_forcelist=Retry.RETRY_AFTER_STATUS_CODES)session.mount('http://', HTTPAdapter(max_retries=retries))session.mount('https://', HTTPAdapter(max_retries=retries))session.headers.update({'Authorization': f"Bearer {prodia_token}"})set -euo pipefailYou’re now ready to make some API calls!
Get a mask for the background pixels
Section titled “Get a mask for the background pixels”npm install sharp --saveconst { createProdia } = require("prodia/v2");const sharp = require("sharp");
const prodia = createProdia({  token: process.env.PRODIA_TOKEN,});
(async () => {  // get input image  const inputBuffer = await (await fetch("https://docs.prodia.com/flux-styles-output.jpg")).arrayBuffer();  await sharp(inputBuffer).toFile("flux-styles-output.jpg");
  // run a flux schnell generation  const job = await prodia.job({    type: "inference.mask-background.v1",  }, {    inputs: [ inputBuffer ]  });
  const maskBuffer = await job.arrayBuffer();  await sharp(maskBuffer).toFile("mask.png");})();node main.jsfrom requests.adapters import HTTPAdapter, Retryfrom io import BytesIOimport jsonimport osimport requestsimport sys
prodia_token = os.getenv('PRODIA_TOKEN')prodia_url = 'https://inference.prodia.com/v2/job'
session = requests.Session()retries = Retry(allowed_methods=None, status_forcelist=Retry.RETRY_AFTER_STATUS_CODES)session.mount('http://', HTTPAdapter(max_retries=retries))session.mount('https://', HTTPAdapter(max_retries=retries))session.headers.update({'Authorization': f"Bearer {prodia_token}"})
try:    with open('flux-styles-output.jpg', 'rb') as f:        input_image = f.read()except FileNotFoundError:    res = requests.get('https://docs.prodia.com/flux-styles-output.jpg')    input_image = BytesIO(res.content)    with open('flux-styles-output.jpg', 'wb') as f:        f.write(res.content)except Exception as e:    raise e
headers = {    'Accept': 'image/png',}
job = {    'type': 'inference.mask-background.v1',}
files = [    ('job', ('job.json', BytesIO(json.dumps(job).encode('utf-8')), 'application/json')),    ('input', ('flux-styles-output.jpg', input_image, 'image/jpeg')),]
res = session.post(prodia_url, headers=headers, files=files)print(f"Request ID: {res.headers['x-request-id']}")print(f"Status: {res.status_code}")
if res.status_code != 200:    print(res.text)    sys.exit(1)
with open('mask.png', 'wb') as f:    f.write(res.content)python main.pyset -euo pipefail
cat <<EOF > job.json{  "type": "inference.mask-background.v1"}EOF
if [[ ! -f flux-styles-output.jpg ]]; then  curl -Lo flux-styles-output.jpg 'https://docs.prodia.com/flux-styles-output.jpg'fi
curl -sSf --retry 3 \  -H "Authorization: Bearer $PRODIA_TOKEN" \  -H 'Accept: image/png' \  --output mask.png \  https://inference.prodia.com/v2/jobbash main.shopen mask.pngxdg-open mask.pngstart mask.png
Selecting the foreground
Section titled “Selecting the foreground”npm install sharp --saveconst { createProdia } = require("prodia/v2");const sharp = require("sharp");
const prodia = createProdia({  token: process.env.PRODIA_TOKEN,});
(async () => {  // get input image  const inputBuffer = await (await fetch("https://docs.prodia.com/flux-styles-output.jpg")).arrayBuffer();  await sharp(inputBuffer).toFile("flux-styles-output.jpg");
  // run a flux schnell generation  const job = await prodia.job({    type: "inference.mask-background.v1",  }, {    inputs: [ inputBuffer ]  });
  const maskBuffer = await job.arrayBuffer();  await sharp(maskBuffer).toFile("mask.png");
  let foregroundImage = await sharp(inputBuffer).ensureAlpha().joinChannel(maskBuffer);  await foregroundImage.toFile("foreground.png");})();node main.jspip install pillowfrom requests.adapters import HTTPAdapter, Retryfrom io import BytesIOfrom PIL import Imageimport jsonimport osimport requestsimport sys
prodia_token = os.getenv('PRODIA_TOKEN')prodia_url = 'https://inference.prodia.com/v2/job'
session = requests.Session()retries = Retry(allowed_methods=None, status_forcelist=Retry.RETRY_AFTER_STATUS_CODES)session.mount('http://', HTTPAdapter(max_retries=retries))session.mount('https://', HTTPAdapter(max_retries=retries))session.headers.update({'Authorization': f"Bearer {prodia_token}"})
try:    with open('flux-styles-output.jpg', 'rb') as f:        input_image = f.read()except FileNotFoundError:    res = requests.get('https://docs.prodia.com/flux-styles-output.jpg')    input_image = BytesIO(res.content)    with open('flux-styles-output.jpg', 'wb') as f:        f.write(res.content)except Exception as e:    raise e
headers = {    'Accept': 'image/png',}
job = {    'type': 'inference.mask-background.v1',}
files = [    ('job', ('job.json', BytesIO(json.dumps(job).encode('utf-8')), 'application/json')),    ('input', ('flux-styles-output.jpg', input_image, 'image/jpeg')),]
res = session.post(prodia_url, headers=headers, files=files)print(f"Request ID: {res.headers['x-request-id']}")print(f"Status: {res.status_code}")
if res.status_code != 200:    print(res.text)    sys.exit(1)
with open('mask.png', 'wb') as f:    f.write(res.content)    mask = Image.open(BytesIO(res.content))
image = Image.open(input_image)image.putalpha(mask)image.save('foreground.png')python main.pyapt install imagemagickset -euo pipefail
cat <<EOF > job.json{  "type": "inference.mask-background.v1"}EOF
if [[ ! -f flux-styles-output.jpg ]]; then  curl -Lo flux-styles-output.jpg 'https://docs.prodia.com/flux-styles-output.jpg'fi
curl -sSf --retry 3 \  -H "Authorization: Bearer $PRODIA_TOKEN" \  -H 'Accept: image/png' \  --output mask.png \  https://inference.prodia.com/v2/job
magick flux-styles-output.jpg mask.png -alpha Off -compose CopyOpacity -composite foreground.pngbash main.shopen foreground.pngxdg-open foreground.pngstart foreground.png