Using the user interface, I sent an image to ChatGPT and then asked it to generate a totally new image, incorporating the image I sent. For example, uploading my profile picture and then ask ChatGPT to generate an image of a beautiful lake, and I am sitting near it reading a book.
I managed to do it from the user interface, but do not find a way to do it using API.
My project is written in NodeJS, so I use openai library. However, I can use axios in order to access directly API if necessary.
1 Like
Hey, @guy_aloni, welcome to the community!
And congrats on moving from ChatGPT to the API!
If you’ve not seen it yet, I recommend the Cookbook page for gpt-image-1…
Do you have code that you’re stumbling with, or are you just wondering where to start?
We’ve got a few gpt-image-1 threads already. Hope you stick around!
1 Like
Echoing @PaulBellow, you can do it. Read the link above.
Even you can upload two images and combine, but it changes something:
Originals:
Output:
Or If you use a mask background:
1 Like
It looks great, can you please share the code for NodeJs please?
Thanks @PaulBellow , very interesting.
I do not manage to do the same in NodeJs, and do not find any reference. Can you help me with that please?
Code
const fs = require("fs");
const path = require("path");
const dotenv = require("dotenv");
const axios = require("axios");
const FormData = require("form-data");
dotenv.config();
const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
const outputDir = "combined_results";
const prompt = "Combine the portrait of me and my father with the lake background. We are sitting on the grass reading books peacefully with the lake in the background.";
// Images I want to combine
const imagePaths = [
"test1.png", // People
"test2.png", // Background
];
function getTimestampedFilename(label = "combined") {
const now = new Date();
const timestamp = now.toISOString().replace(/[-:T]/g, "").slice(0, 15);
return `${label}_${timestamp}.png`;
}
(async () => {
try {
if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir);
const form = new FormData();
imagePaths.forEach((imgPath) => {
form.append("image[]", fs.createReadStream(imgPath));
});
form.append("prompt", prompt);
form.append("model", "gpt-image-1");
form.append("size", "1536x1024");
form.append("quality", "high");
form.append("background", "opaque");
console.log("đź§ Sending request to OpenAI API...");
const response = await axios.post("https://api.openai.com/v1/images/edits", form, {
headers: {
Authorization: `Bearer ${OPENAI_API_KEY}`,
...form.getHeaders(),
},
});
const base64 = response.data.data[0].b64_json;
const filename = getTimestampedFilename("father-lake-scene");
const outputPath = path.join(outputDir, filename);
fs.writeFileSync(outputPath, Buffer.from(base64, "base64"));
console.log("âś… Combined image saved as", outputPath);
} catch (error) {
const msg = error.response?.data?.error?.message || error.message;
console.error("❌ Error:", msg);
}
})();
2 Likes