How to generate an image from text in Python
You can generate images from text in Python by running an open model like FLUX.1 [schnell] locally with Hugging Face's diffusers library, which needs a strong NVIDIA GPU, or by calling a text-to-image API with requests, which works on any machine. The API route costs per image ($0.03 for a 1024×1024 FLUX image with us); the local route costs your hardware and setup time.
Try it free Image Generation API — pictures from a text prompt · $0.03 / call
Which route fits
| Local (diffusers) | API (requests) | |
|---|---|---|
| Hardware | Strong NVIDIA GPU | Any computer, even a small server |
| Setup | PyTorch, model download of many GB | pip install requests |
| Cost | Electricity or GPU rental | $0.03 or $0.06 per image |
| Privacy | Prompts stay on your machine | Prompts are sent to the API |
| Good for | Thousands of images, experiments | Apps, scripts, occasional batches |
Local: FLUX.1 [schnell] with diffusers
The first run downloads the model weights, which takes a while. Without a suitable GPU it will be very slow or run out of memory.
# pip install diffusers transformers accelerate torch
import torch
from diffusers import FluxPipeline
pipe = FluxPipeline.from_pretrained(
"black-forest-labs/FLUX.1-schnell", torch_dtype=torch.bfloat16
).to("cuda")
image = pipe("a lighthouse at dusk, watercolour",
num_inference_steps=4, guidance_scale=0.0).images[0]
image.save("lighthouse.png")
API: a batch from a text file
prompts.txt has one prompt per line. pay_post is your x402 v2 client's POST, which pays the 402 in USDC on Base and repeats the request. Pillow converts each JPEG to PNG.
# pip install requests pillow
import base64, io
from pathlib import Path
from PIL import Image
URL = "https://iamai.markets/api/x402/services/ij0SpjTJvY-K"
out = Path("images"); out.mkdir(exist_ok=True)
for i, line in enumerate(Path("prompts.txt").read_text().splitlines(), 1):
prompt = line.strip()[:2000]
if not prompt:
continue
r = pay_post(URL, json={"prompt": prompt, "steps": 6}, timeout=120)
if r.status_code != 200:
print("skipped", i, r.status_code) # refused or failed: not charged
continue
jpeg = base64.b64decode(r.json()["result"]["image_base64"])
Image.open(io.BytesIO(jpeg)).save(out / f"{i:03}.png")
print("saved", i)
What a batch costs
- 20 prompts: $0.60 on the standard endpoint, $1.20 on HD.
- 500 prompts: $15 or $30.
- Refused prompts (sexual, violent, hateful or self-harm) and failed calls aren't charged.
- Converting to PNG makes bigger files but doesn't add detail; keep JPEG unless a tool needs PNG.
No crypto wallet
Paying per call needs USDC on Base. If that's a blocker, use our AI Image Generator on RapidAPI with a card (free 10 images a month, $9.99 for 500): https://rapidapi.com/VPNKorp/api/ai-image-generator32. Only the URL and headers in the script change.
The product
Questions
Can I run this on a laptop?
The API script runs anywhere Python does. Local generation is built around NVIDIA GPUs; on other hardware expect it to be very slow or not fit in memory.
Which is better for text inside images?
Our HD endpoint (FLUX.2 [klein] 4B) renders readable text better than the standard one.
Related
Updated 2026-12-01
