0
Create image variation
One script reply has been approved by the moderators Verified

Creates a variation of a given image.

Created by hugo697 282 days ago Viewed 8955 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 282 days ago
1
type Openai = {
2
  api_key: string;
3
  organization_id: string;
4
};
5
type Base64 = string;
6
/**
7
 * Create image variation
8
 * Creates a variation of a given image.
9
 */
10
export async function main(
11
  auth: Openai,
12
  body: {
13
    image: {
14
      base64: Base64;
15
      type:
16
        | "image/png"
17
        | "image/jpeg"
18
        | "image/gif"
19
        | "application/pdf"
20
        | "appication/json"
21
        | "text/csv"
22
        | "text/plain"
23
        | "audio/mpeg"
24
        | "audio/wav"
25
        | "video/mp4";
26
      name: string;
27
    };
28
    model?: string | "dall-e-2";
29
    n?: number;
30
    response_format?: "url" | "b64_json";
31
    size?: "256x256" | "512x512" | "1024x1024";
32
    user?: string;
33
    [k: string]: unknown;
34
  }
35
) {
36
  const url = new URL(`https://api.openai.com/v1/images/variations`);
37

38
  const formData = new FormData();
39
  for (const [k, v] of Object.entries(body)) {
40
    if (v !== undefined && v !== "") {
41
      if (["image"].includes(k)) {
42
        const { base64, type, name } = v as {
43
          base64: Base64;
44
          type: string;
45
          name: string;
46
        };
47
        formData.append(
48
          k,
49
          new Blob([Uint8Array.from(atob(base64), (m) => m.codePointAt(0)!)], {
50
            type,
51
          }),
52
          name
53
        );
54
      } else {
55
        formData.append(k, String(v));
56
      }
57
    }
58
  }
59
  const response = await fetch(url, {
60
    method: "POST",
61
    headers: {
62
      "OpenAI-Organization": auth.organization_id,
63
      Authorization: "Bearer " + auth.api_key,
64
    },
65
    body: formData,
66
  });
67
  if (!response.ok) {
68
    const text = await response.text();
69
    throw new Error(`${response.status} ${text}`);
70
  }
71
  return await response.json();
72
}
73