From 55f9d752f285a436496340fab35a7fb155907dca Mon Sep 17 00:00:00 2001 From: Adekunle Date: Mon, 30 Jun 2025 22:21:58 +0100 Subject: [PATCH 1/2] (example) generate picture --- examples/picture.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 examples/picture.ts diff --git a/examples/picture.ts b/examples/picture.ts new file mode 100644 index 000000000..5397cf7e1 --- /dev/null +++ b/examples/picture.ts @@ -0,0 +1,21 @@ +#!/usr/bin/env -S npm run tsn -T +import OpenAI from 'openai'; + +// gets API Key from environment variable OPENAI_API_KEY +const openai = new OpenAI(); + +async function main() { + // Generate an image based on the prompt + const response = await openai.images.generate({ + model: 'dall-e-3', + prompt: 'An astronaut lounging in a tropical resort in space, pixel art', + }); + console.log(response); // Log the response to the console, a URL link to image will be in the response.data[0].url + if (response.data && response.data[0] && response.data[0].url) { + console.log('Image URL:', response.data[0].url); // Log the image URL + } else { + console.log('Image URL not found in response.'); + } +} + +main().catch(console.error); From 3298c96e63f7c212dff5a5f1f52b1c2f1e7deb86 Mon Sep 17 00:00:00 2001 From: Adekunle Date: Wed, 2 Jul 2025 23:47:35 +0100 Subject: [PATCH 2/2] (example) generate and edit picture --- examples/picture.ts | 70 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 60 insertions(+), 10 deletions(-) diff --git a/examples/picture.ts b/examples/picture.ts index 5397cf7e1..43621a6b8 100644 --- a/examples/picture.ts +++ b/examples/picture.ts @@ -1,20 +1,70 @@ #!/usr/bin/env -S npm run tsn -T -import OpenAI from 'openai'; +import OpenAI, { toFile } from 'openai'; +import fs from 'fs'; +/* +This example shows how to generate images from text prompts, as well as how to generate an image from a prompt and an input image. + +if you are working with solution 1 alone, you can remove {toFile} from the openai import on line 2. +*/ // gets API Key from environment variable OPENAI_API_KEY const openai = new OpenAI(); - async function main() { - // Generate an image based on the prompt - const response = await openai.images.generate({ - model: 'dall-e-3', + // [1] Generate an image based on the prompt + await createImage(); + // [2] Generate an image with edit based on prompt and input image + await createImageEdit(); +} +/* +[1] The code below is used to create an image based on prompt. + n-is the number of images to be generated, and size is the size of the image to be generated. + size- can be one of the following: '256x256', '512x512', or '1024x1024'. + both size and n are optional parameters, if not provided, the default values will be used. + The generated image will be saved to a file named output.png + */ +async function createImage() { + const img = await openai.images.generate({ + model: 'gpt-image-1', // prompt: 'An astronaut lounging in a tropical resort in space, pixel art', + n: 1, + size: '1024x1024', }); - console.log(response); // Log the response to the console, a URL link to image will be in the response.data[0].url - if (response.data && response.data[0] && response.data[0].url) { - console.log('Image URL:', response.data[0].url); // Log the image URL - } else { - console.log('Image URL not found in response.'); + // Save the image to a file, you can do console.log(img) to see the response structure + if (img.data && img.data[0] && img.data[0].b64_json) { + const imageBuffer = Buffer.from(img.data[0].b64_json, 'base64'); + fs.writeFileSync('output.png', imageBuffer); + } +} +/* +[2] The code below is used to create an image edit based on the input images and prompt. + */ +// List of image files to be used for the image edit, this should be in the same directory as this script +const imageFiles: string[] = ['bath-bomb.png', 'body-lotion.png', 'incense-kit.png', 'soap.png']; + +/** + * Processes multiple image files and converts them to File objects + * using OpenAI's toFile utility for API compatibility + */ +async function createImageEdit(): Promise { + const images: File[] = await Promise.all( + imageFiles.map( + async (file: string): Promise => + await toFile(fs.createReadStream(file), null, { + type: 'image/png', + }), + ), + ); + + const img: OpenAI.Images.ImagesResponse = await openai.images.edit({ + model: 'gpt-image-1', + image: images, + prompt: 'Create a lovely gift basket with these four items in it', + }); + + // Save the image to a file, you can do console.log(img) to see the response structure + if (img.data && img.data[0] && img.data[0].b64_json) { + const imageBuffer: Buffer = Buffer.from(img.data[0].b64_json, 'base64'); + fs.writeFileSync('output2.png', imageBuffer); } }