Skip to content

Commit 555f308

Browse files
authored
Fix createImageVariantion and createImageEdit (#21)
* Fix createImageVariantion and createImageEdit * Fix response format * Add examples
1 parent 9c6c51e commit 555f308

4 files changed

Lines changed: 87 additions & 16 deletions

File tree

examples/imageEditWithFiles.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { OpenAI } from "../mod.ts";
2+
3+
const openAI = new OpenAI(Deno.env.get("YOUR_API_KEY")!);
4+
5+
const imageBuffer = await Deno.open("@otter.png", { read: true });
6+
const maskBuffer = await Deno.open("@mask.png", { read: true });
7+
const imageEdit = await openAI.createImageEdit({
8+
image: imageBuffer,
9+
mask: maskBuffer,
10+
prompt: "A cute baby sea otter wearing a beret",
11+
n: 2,
12+
size: "1024x1024",
13+
});
14+
15+
console.log(imageEdit);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { OpenAI } from "../mod.ts";
2+
3+
const openAI = new OpenAI(Deno.env.get("YOUR_API_KEY")!);
4+
5+
const imageBuffer = await Deno.open("@otter.png", { read: true });
6+
const imageVariation = await openAI.createImageVariation({
7+
image: imageBuffer,
8+
n: 2,
9+
size: "1024x1024",
10+
});
11+
12+
console.log(imageVariation);

src/openai.ts

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,36 @@ export class OpenAI {
267267
* https://platform.openai.com/docs/api-reference/images/create-edit
268268
*/
269269
async createImageEdit(options: ImageEditOptions): Promise<Image> {
270-
return await this.#request(`/images/edits`, {
271-
image: options.image,
272-
mask: options.mask,
273-
prompt: options.prompt,
274-
n: options.n,
275-
size: options.size,
276-
response_format: options.responseFormat,
277-
user: options.user,
270+
const formData = new FormData();
271+
272+
// Model specified
273+
formData.append("image", options.image);
274+
275+
// File data
276+
if (typeof options.image === "string") {
277+
const fileData = await Deno.readFile(options.image);
278+
279+
formData.append(
280+
"image",
281+
new File([fileData], basename(options.image)),
282+
);
283+
} else {
284+
// Deno types are wrong
285+
formData.append("image", options.image as unknown as Blob);
286+
}
287+
288+
if (options.n) { formData.append("n", options.n); }
289+
if (options.mask) { formData.append("mask", options.mask); }
290+
if (options.prompt) { formData.append("prompt", options.prompt); }
291+
if (options.size) { formData.append("size", options.size); }
292+
if (options.user) { formData.append("user", options.user); }
293+
if (options.responseFormat) {
294+
formData.append("response_format", options.responseFormat);
295+
}
296+
297+
return await this.#request(`/images/edits`, formData, {
298+
noContentType: true,
299+
method: "POST",
278300
});
279301
}
280302

@@ -284,12 +306,34 @@ export class OpenAI {
284306
* https://platform.openai.com/docs/api-reference/images/create-variation
285307
*/
286308
async createImageVariation(options: ImageVariationOptions): Promise<Image> {
287-
return await this.#request(`/images/variations`, {
288-
image: options.image,
289-
n: options.n,
290-
size: options.size,
291-
response_format: options.responseFormat,
292-
user: options.user,
309+
const formData = new FormData();
310+
311+
// Model specified
312+
formData.append("image", options.image);
313+
314+
// File data
315+
if (typeof options.image === "string") {
316+
const fileData = await Deno.readFile(options.image);
317+
318+
formData.append(
319+
"image",
320+
new File([fileData], basename(options.image)),
321+
);
322+
} else {
323+
// Deno types are wrong
324+
formData.append("image", options.image as unknown as Blob);
325+
}
326+
327+
if (options.n) { formData.append("n", options.n); }
328+
if (options.size) { formData.append("size", options.size); }
329+
if (options.user) { formData.append("user", options.user); }
330+
if (options.responseFormat) {
331+
formData.append("response_format", options.responseFormat);
332+
}
333+
334+
return await this.#request(`/images/variations`, formData, {
335+
noContentType: true,
336+
method: "POST",
293337
});
294338
}
295339

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ export interface ImageEditOptions {
372372
* If mask is not provided, image must have transparency, which will be used as the mask.
373373
* https://platform.openai.com/docs/api-reference/images/create-edit#images/create-edit-image
374374
*/
375-
image: string;
375+
image: FileSpecifier;
376376

377377
/**
378378
* An additional image whose fully transparent areas (e.g. where alpha is zero) indicate where image should be edited.
@@ -417,7 +417,7 @@ export interface ImageVariationOptions {
417417
* The image to edit. Must be a valid PNG file, less than 4MB, and square.
418418
* https://platform.openai.com/docs/api-reference/images/create-variation#images/create-variation-image
419419
*/
420-
image: string;
420+
image: FileSpecifier;
421421

422422
/**
423423
* The number of images to generate. Must be between 1 and 10.

0 commit comments

Comments
 (0)