AI E-commerce Product Image Generation
AI product image generation reduces photography session costs: background removal, background replacement, lifestyle scene generation with product, color/material variations without reshoot. Typical savings: $50–200 per product vs. studio shoot.
Background Removal + Generation
from rembg import remove, new_session
from PIL import Image
from diffusers import StableDiffusionXLInpaintPipeline
import torch
import io
import numpy as np
class ProductImageGenerator:
def __init__(self):
self.bg_remover = new_session("isnet-general-use")
self.pipe = StableDiffusionXLInpaintPipeline.from_pretrained(
"diffusers/stable-diffusion-xl-1.0-inpainting-0.1",
torch_dtype=torch.float16
).to("cuda")
def remove_background(self, product_image: bytes) -> tuple[bytes, bytes]:
"""Return (image without background, background mask)"""
result = remove(product_image, session=self.bg_remover)
img_rgba = Image.open(io.BytesIO(result)).convert("RGBA")
# Mask: white = background (for inpainting), black = product
r, g, b, a = img_rgba.split()
mask = Image.fromarray(255 - np.array(a)) # invert alpha
img_rgb = Image.new("RGB", img_rgba.size, (255, 255, 255))
img_rgb.paste(img_rgba, mask=img_rgba.split()[3])
img_buf = io.BytesIO()
img_rgb.save(img_buf, format="PNG")
mask_buf = io.BytesIO()
mask.save(mask_buf, format="PNG")
return img_buf.getvalue(), mask_buf.getvalue()
def place_on_background(self, product_image: bytes, background_prompt: str, steps: int = 30) -> bytes:
product_bytes, mask_bytes = self.remove_background(product_image)
result = self.pipe(
prompt=background_prompt,
image=Image.open(io.BytesIO(product_bytes)),
mask_image=Image.open(io.BytesIO(mask_bytes)),
num_inference_steps=steps,
guidance_scale=9.0,
strength=0.99
).images[0]
buf = io.BytesIO()
result.save(buf, format="PNG")
return buf.getvalue()
Lifestyle Scenes Generation
Category-based prompts: electronics (desk setups), clothing (street style), food (rustic tables), cosmetics (luxury aesthetics). API generates 4-6 product scene variations per image. Integration with product database: SKU → category → scene prompts.
Timeline: setup pipeline — 1–2 days. Full platform with bulk processing, API, front-end — 2–3 weeks.







