🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)
🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)

🔧 Programmierung 🕛 kürzlich 4 Min Lesezeit
0

Replacing Only the Background of an Image with AI Generation Using the Stable Diffusion Web API

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht




Introduction



This guide demonstrates how to replace the background of an image using Python code only, without relying on image editing software like Photoshop. The goal is to keep the subject intact while swapping in an AI-generated background.



While this approach may not be revolutionary, it addresses a common need, so I hope it will be helpful for those with similar requirements.






Input and Output Images



Let’s start with the results.



The following output image was generated from the input image shown below.






Input Image








Libraries



Install Web API.



To access this API, you’ll need to obtain an API Key from their page.



To keep your key secure, save it as an environment variable rather than hardcoding it in your code.



In my environment, I use the zshrc settings file.




CODE
$ open ~/.zshrc






I saved the key under the name STABILITY_API_KEY.




CODE
export STABILITY_API_KEY=your_api_key_here









Code



Here, we use the to create the new background.



The prompt used is "Large glass windows with a view of the metropolis behind"




CODE
import os
import requests

# File paths
input_path = './input.png' # Original image
mask_path = './mask.png' # Mask image (temporarily generated)
output_path = './output.png' # Output image

# Check for API Key
api_key = os.getenv("STABILITY_API_KEY")
if api_key is None:
raise Exception("Missing Stability API key.")

headers = {
"Accept": "image/*",
"Authorization": f"Bearer {api_key}"
}

# Call Remove Background API
response = requests.post(
f"https://api.stability.ai/v2beta/stable-image/edit/remove-background",
headers=headers,
files={
"image": open(input_path, "rb")
},
data={
"output_format": "png"
},
)

# Save mask image
if response.status_code == 200:
with open(mask_path, 'wb') as file:
file.write(response.content)
else:
raise Exception(str(response.json()))

# Call Inpaint API
response = requests.post(
"https://api.stability.ai/v2beta/stable-image/edit/inpaint",
headers=headers,
files={
"image": open(mask_path, "rb"),
},
data={
"prompt": "Large glass windows with a view of the metropolis behind",
"output_format": "png",
"grow_mask": 0, # Disable blurring around the mask
},
)

# Delete mask image
os.remove(mask_path)

# Save output image
if response.status_code == 200:
with open(output_path, "wb") as file:
file.write(response.content)
else:
raise Exception(str(response.json()))









Using rembg



Another approach for background removal is to use



If you set up a local Stable Diffusion environment, you can eliminate API call costs, so feel free to explore that option if it suits your needs.






Conclusion



Being able to achieve this through code alone is highly convenient.



It’s exciting to witness the ongoing improvements in workflow efficiency.






Original Japanese Article



Stable DiffusionのWeb APIを用いて画像上の人物はそのままに背景だけをAI生成で入れ替えてみた

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
GPT-6 Astra Release Today? OpenAI’s Next Major AI Model Is Almost Here
1 Quelle
Apple accuses OpenAI of destroying evidence as trade-secrets fight intensifies
1 Quelle
Major AI platforms go down in unprecedented simultaneous outage
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Replacing Only the Background of an Image with AI Generation Using the Stable Diffusion Web API

Thematisch verwandte Begriffe: Replacing, Only, Background, Image · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...