Generate synthetic images for your Roboflow project.
To generate synthetic images, make a POST request to the following API endpoint:
POST https://api.roboflow.com/synthetic-image
The only supported models are OpenAI's generative image models. To use this endpoint, you will need to configure your third party OpenAI API key in Workspace Settings.
For requests with an image parameter we first send the image to the GPT-4 Vision model to generate an image description. The generative image prompt is a combination of the image description, the prompt parameter, and additional prompt keywords to make the image photorealistic.
You can find a Roboflow image ID with the search API, or by opening the image in Roboflow and copying the ID from the URL which is in this format:
curl-XPOST"https://api.roboflow.com/synthetic-image?api_key=$ROBOFLOW_API_KEY" \-H 'Content-Type: application/json' \--data \'{ "image": "62MYRuqqWCRZ02q52vsY", "image_type": "id", "project_url": "fall-leaves", "prompt": "All trees have red leaves."}'
Generating a new image from prompt
curl-XPOST"https://api.roboflow.com/synthetic-image?api_key=$ROBOFLOW_API_KEY" \-H 'Content-Type: application/json' \--data \'{ "project_url": "fall-leaves", "prompt": "Image of a residential setting during fall with dominant maple trees showcasing intense orange leaves, the only color of tree leaf."}'
Generating an image based on a local image (e.g., trees.png)
# Convert the image to base64IMAGE_BASE64=$(opensslbase64-intrees.png)# Create a temporary fileTEMP_FILE=$(mktemp)# Write the JSON data to the temporary fileecho'{ "image": "'"$IMAGE_BASE64"'", "image_type": "base64", "project_url": "fall-leaves", "prompt": "All trees have red leaves."}'> $TEMP_FILE# Use the temporary file as the data parameter in the curl commandcurl-XPOST"https://api.roboflow.com/synthetic-image?api_key=$ROBOFLOW_API_KEY" \-H 'Content-Type: application/json' \--data @${TEMP_FILE}# Remove the temporary filerm $TEMP_FILE
We're using axios to perform the POST request in this example so first run npm install axios to install the dependency.
Generating an image based on a Roboflow image
constaxios=require("axios");axios({ method:"POST", url:"https://api.roboflow.com/synthetic-image", params: { api_key:"ROBOFLOW_API_KEY", }, data: { image:"62MYRuqqWCRZ02q52vsY", image_type:"id", project_url:"fall-leaves", prompt:"All trees have red leaves." }, headers: {"Content-Type":"application/json" }}).then(function(response) {console.log(response.data);}).catch(function(error) {console.log(error.message);});
Generating a new image from prompt
constaxios=require("axios");axios({ method:"POST", url:"https://api.roboflow.com/synthetic-image", params: { api_key:"ROBOFLOW_API_KEY", }, data: { project_url:"fall-leaves", prompt:"Image of a residential setting during fall with dominant maple trees showcasing intense orange leaves, the only color of tree leaf." }, headers: {"Content-Type":"application/json" }}).then(function(response) {console.log(response.data);}).catch(function(error) {console.log(error.message);});
Generating an image based on a local image (e.g., trees.png)
constfs=require("fs");constaxios=require("axios");// Read the image file and convert it to base64constimageBase64=fs.readFileSync("trees.png", { encoding:"base64" });// Define the data for the POST requestconstdata= { image: imageBase64, image_type:"base64", project_url:"fall-leaves", prompt:"All trees have red leaves."};// Send the POST requestaxios.post("https://api.roboflow.com/synthetic-image?api_key=ROBOFLOW_API_KEY", data, { headers: {"Content-Type":"application/json" } }).then((response) => {console.log(response.data); }).catch((error) => {console.error(error); });