Skip to content

zouari-oss/img2img-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Contributors Forks Stargazers Issues GPL3.0 License Linkedin

img2img-api.png

img2img-api

Generate images from input images + prompts using Stable Diffusion (Anything V5)

OverviewKey FeaturesAPI ExampleUsageDownloadEmailwareContributingLicenseContactAcknowledgments

screenshot

Overview

img2img-api exposes a simple HTTP API to generate images from an input image plus text prompts using Stable Diffusion (model: Anything V5). It accepts an initial URL image with generation parameters and returns generated images as URL.

Key Features

  • FastAPI HTTP API with OpenAPI docs available at /docs
  • Img2Img generation using Stable Diffusion (Anything V5) via diffusers' StableDiffusionImg2ImgPipeline
  • Accepts an image URL (HTTP/HTTPS); server downloads the input image for processing
  • Generated images are uploaded to a configured host and the API returns the hosted image URL
  • Configurable via environment variables: MODEL_PATH, DEVICE, FREEIMAGE_API_KEY, FREEIMAGE_UPLOAD_URL
  • GPU support (CUDA) with automatic float16 when available; attention slicing enabled for memory efficiency
  • Request validation with Pydantic schemas and clear JSON responses on success/error
  • CORS enabled for browser-based clients; small, dependency-light service suitable for local or server use

API Example

POST /api/generate
Content-Type: application/json

This project expects a JSON body with an image_url pointing to a publicly-accessible image. The server will download that image, run img2img using the configured Stable Diffusion model, upload the result to the configured free image host, and return the hosted URL.

Request (JSON example):

{
  "image_url": "https://example.com/input.jpg",
  "prompt": "A fantasy landscape painting, vibrant colors, highly detailed",
  "negative_prompt": "lowres, blurry",
  "strength": 0.55,
  "guidance_scale": 5.0,
  "num_inference_steps": 4
}

Response (JSON example):

{
  "status": "success",
  "image_url": "https://freeimage.host/abcd1234.png"
}

Note

  • image_url must be an HTTP(S) URL reachable by the server (no local file paths or data URLs).
  • The service requires FREEIMAGE_API_KEY and FREEIMAGE_UPLOAD_URL to be configured so the generated image can be uploaded and hosted.

Usage

Run locally (development):

git clone https://github.com/zouari-oss/img2img-api && cd img2img-api/project
chmod u+x setup && ./setup
fastapi run

Important

Base URL (local): http://localhost:8000

curl (JSON with image_url)

curl -X POST "http://localhost:8000/api/generate" \
  -H "Content-Type: application/json" \
  -d '{
    "image_url":"https://example.com/input.jpg",
    "prompt":"A fantasy landscape painting, vibrant colors, highly detailed",
    "negative_prompt":"lowres, blurry",
    "strength":0.55,
    "guidance_scale":5.0,
    "num_inference_steps":4
  }'

JavaScript (fetch, Node/browser)

fetch("http://localhost:8000/api/generate", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    image_url: "https://example.com/input.jpg",
    prompt: "A fantasy landscape painting, vibrant colors, highly detailed",
    negative_prompt: "lowres, blurry",
    strength: 0.55,
    guidance_scale: 5.0,
    num_inference_steps: 4,
  }),
})
  .then((res) => res.json())
  .then((data) => console.log("Hosted URL:", data.image_url))
  .catch(console.error);

PHP (curl)

<?php
$ch = curl_init("http://localhost:8000/api/generate");
$data = json_encode([
  "image_url" => "https://example.com/input.jpg",
  "prompt" => "A fantasy landscape painting, vibrant colors, highly detailed",
  "negative_prompt" => "lowres, blurry",
  "strength" => 0.55,
  "guidance_scale" => 5.0,
  "num_inference_steps" => 4
]);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
var_dump(json_decode($response, true));
?>

Java (HttpURLConnection)

URL url = new URL("http://localhost:8000/api/generate");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);

String jsonInputString = "{"
  + "\"image_url\":\"https://example.com/input.jpg\","
  + "\"prompt\":\"A fantasy landscape painting, vibrant colors, highly detailed\","
  + "\"negative_prompt\":\"lowres, blurry\","
  + "\"strength\":0.55,"
  + "\"guidance_scale\":5.0,"
  + "\"num_inference_steps\":4"
  + "}";

try(OutputStream os = conn.getOutputStream()) {
  byte[] input = jsonInputString.getBytes("utf-8");
  os.write(input, 0, input.length);
}

try(BufferedReader br = new BufferedReader(
    new InputStreamReader(conn.getInputStream(), "utf-8"))) {
  StringBuilder response = new StringBuilder();
  String responseLine;
  while ((responseLine = br.readLine()) != null) {
    response.append(responseLine.trim());
  }
  System.out.println(response.toString());
}

C (libcurl)

#include <stdio.h>
#include <curl/curl.h>

int main(void){
  CURL *curl = curl_easy_init();
  if(curl) {
    struct curl_slist *headers = NULL;
    headers = curl_slist_append(headers, "Content-Type: application/json");

    const char *data =
      "{"
      "\"image_url\":\"https://example.com/input.jpg\","
      "\"prompt\":\"A fantasy landscape painting, vibrant colors, highly detailed\","
      "\"negative_prompt\":\"lowres, blurry\","
      "\"strength\":0.55,"
      "\"guidance_scale\":5.0,"
      "\"num_inference_steps\":4"
      "}";

    curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8000/api/generate");
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);

    CURLcode res = curl_easy_perform(curl);
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));

    curl_slist_free_all(headers);
    curl_easy_cleanup(curl);
  }
  return 0;
}

Download

You can download the latest installable version of img2img-api for Windows, macOS and Linux.

Emailware

img2img-api is an emailware. Meaning, if you liked using this app or it has helped you in any way, would like you send as an email at zouariomar20@gmail.com about anything you'd want to say about this software. I'd really appreciate it!

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This repository is licensed under the GPL-3.0 License. You are free to use, modify, and distribute the content. See the LICENSE file for details.

Contact

For questions or suggestions, feel free to reach out:

Acknowledgments

Built with ❤️ for the open-source community.