OpenAI の新しい画像生成API(gpt-image-1)を試してみた

> 注.) この文章は基本、人間が書いております。ただし、一部AIを使いましたので、AI In⬇️ / AI Out⬆️　を絵文字で明記しております。



# 朝、4時に目を覚ますとOpenAIという謎のAI会社が画像作るAPIを出してたので試してみました。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/30621/5c03c15d-ad74-45f0-8671-f39cac748b70.png)



↓↓説明

https://openai.com/index/image-generation-api/

## ガイド


https://platform.openai.com/docs/guides/image-generation?image-generation-model=gpt-image-1


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/30621/2dbb1aa5-6acb-43c9-a1d4-a4527e8fa89c.png)
俺たちにできないことを平然とやってのける。そこに痺れる。憧れる。




## 要点

⬇️ (AI In)

- gpt-image-1は で **「画像のChatGPT」** といえる存在
- APIから使えるようになったのは商用にも大きい
- 安全対策も万全（Moderationパラメータで調整可）

⬆️ (AI Out)

## 認証が必要（個人情報登録)

なんか、オーガナイゼーション認証が必要でした。犯罪に使われるからでしょうか？

<img width="75%" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/30621/9d1ac22d-e89b-40bd-aebb-a392efe0fa5b.png">


とりあえず、マイナンバーカードでサクッと登録、個人情報を紐づけます。顔面の写真も撮られます。
いわゆる eKYCですね。


<img width="75%" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/30621/99a92a37-9ff5-441a-b895-7e4a7183987f.png">

<img width="75%" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/30621/50304683-d6e5-4d42-9862-ff3a35d20dc1.png">

<img width="75%" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/30621/abdd343c-7630-4b42-8c5c-b222f62fd3be.png">

## 使い方

あとは、PlayGroundでプロンプト適当に入れると絵が生成されます。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/30621/84826452-aa4a-41f6-b839-c7427a72fd36.png)



https://platform.openai.com/playground/images

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/30621/acea36ef-8a3a-4037-98fe-74ee04c3c12a.png)

PlayGroundだとNSFWっぽいのプロンプトはすぐ怒られます。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/30621/c9e08a7d-c121-48f6-95b5-a8222ad396ef.png)

多分コンテンツ モデレーション(moderation)パラメータで倫理観をlowで落とせばOK?

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/30621/5b51fea4-d46d-4177-b6e2-537492e5eed9.png)


## API

APIはこんな感じ

### Python
```python
from openai import OpenAI
client = OpenAI()

response = client.images.generate(
  model="gpt-image-1",
  prompt="anime-style illustration of a magical girl version of a 22-year-old Japanese female college student, long blonde twintails, wearing a sparkly pink and gold magical costume with ribbons, thigh-high boots, holding a glowing wand, on a starry stage with spotlight, cute and confident pose, 7000K lighting, vivid colors, anime background  \n",
  n=1,
  size="1024x1536",
  quality="high"
)
```

### Node.js

```javascript
import OpenAI from "openai";

const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
});

const response = await openai.images.generate({
  model: "gpt-image-1",
  prompt: "anime-style illustration of a magical girl version of a 22-year-old Japanese female college student, long blonde twintails, wearing a sparkly pink and gold magical costume with ribbons, thigh-high boots, holding a glowing wand, on a starry stage with spotlight, cute and confident pose, 7000K lighting, vivid colors, anime background  \n",
  n: 1,
  size: "1024x1536",
  quality: "high"
});
```

### JSON

```json
{
  "model": "gpt-image-1",
  "prompt": "anime-style illustration of a magical girl version of a 22-year-old Japanese female college student, long blonde twintails, wearing a sparkly pink and gold magical costume with ribbons, thigh-high boots, holding a glowing wand, on a starry stage with spotlight, cute and confident pose, 7000K lighting, vivid colors, anime background  \n",
  "n": 1,
  "size": "1024x1536",
  "quality": "high"
}
```

### Curl

```sh
curl https://api.openai.com/v1/images/generations \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
  "model": "gpt-image-1",
  "prompt": "anime-style illustration of a magical girl version of a 22-year-old Japanese female college student, long blonde twintails, wearing a sparkly pink and gold magical costume with ribbons, thigh-high boots, holding a glowing wand, on a starry stage with spotlight, cute and confident pose, 7000K lighting, vivid colors, anime background  \n",
  "n": 1,
  "size": "1024x1536",
  "quality": "high"
}' | jq -r '.data[0].b64_json' | base64 --decode > output.png
```

## 実際に API 使ってみた

### 普通に Curl 

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/30621/7ea3ed32-d22c-474a-b0de-1e9c25d9c95a.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/30621/b5caddb1-a7a3-462a-bfe4-4f5fc3cee0f7.png)


### 透過PNGを出す

> "background": "transparent"

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/30621/707156fe-b1da-49d9-854a-001851abb55b.png)

![iTerm2.Zi2dWR.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/30621/305587c5-7f84-48a5-a509-2596eedc3545.png)


## コスト

結構高いｗ

https://x.com/GOROman/status/1915196647879139430


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/30621/41b31bed-96bf-44f1-82be-3b0435232fb2.png)


## 最後に

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/30621/37593c77-7612-412e-944b-dd8d61dad484.png)

AI情報発信してます！（1日二十円で読める
↓↓↓

https://x.com/GOROman/creator-subscriptions/subscribe