pop-img 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +63 -0
- package/bin/pop-img.js +174 -0
- package/package.json +17 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 alextis59
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# pop-img
|
|
2
|
+
|
|
3
|
+
Generate an image from a prompt using the OpenAI Images API.
|
|
4
|
+
|
|
5
|
+
## Install (global)
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install -g pop-img
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
export OPENAI_API_KEY="..."
|
|
15
|
+
pop-img -o ./out.png "A cute robot barista, watercolor, soft lighting"
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Print-only (no API call):
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pop-img -o ./out.webp --dry-run "A futuristic dashboard UI"
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Overwrite existing file:
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pop-img -o ./assets/hero.png --overwrite "Cyberpunk city skyline at night"
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Options
|
|
31
|
+
|
|
32
|
+
- `-o, --output <path>` (required) Output file path: .png, .jpg/.jpeg, .webp
|
|
33
|
+
- `-m, --model <name>` Image model (default: gpt-image-1 or `$OPENAI_IMAGE_MODEL`)
|
|
34
|
+
- `-s, --size <size>` 256x256 | 512x512 | 1024x1024 (default: 1024x1024)
|
|
35
|
+
- `-q, --quality <quality>` low | medium | high | auto (default: auto)
|
|
36
|
+
- `--background <bg>` opaque | transparent (default: opaque)
|
|
37
|
+
- `--overwrite` overwrite output file if it exists
|
|
38
|
+
- `--dry-run` print resolved settings, do not call the API
|
|
39
|
+
- `-v, --version` output the version
|
|
40
|
+
|
|
41
|
+
## Notes
|
|
42
|
+
|
|
43
|
+
- This tool writes the returned base64 image or downloads a returned URL to the output file.
|
|
44
|
+
|
|
45
|
+
## How to run locally (dev)
|
|
46
|
+
|
|
47
|
+
From the project root:
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
npm install
|
|
51
|
+
OPENAI_API_KEY="..." node bin/pop-img.js -o ./test.png "A cat wearing sunglasses"
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Publish-ready checklist
|
|
55
|
+
|
|
56
|
+
- `npm login`
|
|
57
|
+
- `npm publish`
|
|
58
|
+
|
|
59
|
+
Or install directly from a git repo:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
npm install -g .
|
|
63
|
+
```
|
package/bin/pop-img.js
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { Command } from "commander";
|
|
3
|
+
import { createRequire } from "module";
|
|
4
|
+
import OpenAI from "openai";
|
|
5
|
+
import fs from "fs";
|
|
6
|
+
import path from "path";
|
|
7
|
+
|
|
8
|
+
const require = createRequire(import.meta.url);
|
|
9
|
+
const pkg = require("../package.json");
|
|
10
|
+
|
|
11
|
+
function fail(msg, code = 1) {
|
|
12
|
+
console.error(`pop-img: ${msg}`);
|
|
13
|
+
process.exit(code);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function inferFormatFromPath(outPath) {
|
|
17
|
+
const ext = path.extname(outPath).toLowerCase();
|
|
18
|
+
if (ext === ".png") return "png";
|
|
19
|
+
if (ext === ".jpg" || ext === ".jpeg") return "jpeg";
|
|
20
|
+
if (ext === ".webp") return "webp";
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function ensureDirForFile(filePath) {
|
|
25
|
+
const dir = path.dirname(filePath);
|
|
26
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function downloadToFile(url, outPath) {
|
|
30
|
+
const res = await fetch(url);
|
|
31
|
+
if (!res.ok) {
|
|
32
|
+
fail(`failed to download image: ${res.status} ${res.statusText}`);
|
|
33
|
+
}
|
|
34
|
+
const arrayBuffer = await res.arrayBuffer();
|
|
35
|
+
fs.writeFileSync(outPath, Buffer.from(arrayBuffer));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function main() {
|
|
39
|
+
const program = new Command();
|
|
40
|
+
|
|
41
|
+
program
|
|
42
|
+
.name("pop-img")
|
|
43
|
+
.version(pkg.version, "-v, --version", "Output the version")
|
|
44
|
+
.description(
|
|
45
|
+
'Generate an image via OpenAI. Example: pop-img -o out.png "A cute robot"'
|
|
46
|
+
)
|
|
47
|
+
.argument("<prompt...>", "Text prompt for the image")
|
|
48
|
+
.requiredOption("-o, --output <path>", "Output file path (png/jpg/webp)")
|
|
49
|
+
.option(
|
|
50
|
+
"-m, --model <name>",
|
|
51
|
+
"Image model",
|
|
52
|
+
process.env.OPENAI_IMAGE_MODEL || "gpt-image-1"
|
|
53
|
+
)
|
|
54
|
+
.option(
|
|
55
|
+
"-s, --size <size>",
|
|
56
|
+
"Image size: 256x256 | 512x512 | 1024x1024",
|
|
57
|
+
"1024x1024"
|
|
58
|
+
)
|
|
59
|
+
.option(
|
|
60
|
+
"-q, --quality <quality>",
|
|
61
|
+
"Quality: low | medium | high | auto",
|
|
62
|
+
"auto"
|
|
63
|
+
)
|
|
64
|
+
.option(
|
|
65
|
+
"--background <bg>",
|
|
66
|
+
"Background: opaque | transparent (png/webp recommended)",
|
|
67
|
+
"opaque"
|
|
68
|
+
)
|
|
69
|
+
.option("--overwrite", "Overwrite output file if it exists", false)
|
|
70
|
+
.option("--dry-run", "Print resolved options, don't call the API", false)
|
|
71
|
+
.parse(process.argv);
|
|
72
|
+
|
|
73
|
+
const opts = program.opts();
|
|
74
|
+
const prompt = program.args.join(" ").trim();
|
|
75
|
+
const outPath = path.resolve(process.cwd(), opts.output);
|
|
76
|
+
|
|
77
|
+
if (!prompt) fail("missing prompt");
|
|
78
|
+
if (!process.env.OPENAI_API_KEY) {
|
|
79
|
+
fail("OPENAI_API_KEY is not set in environment");
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const format = inferFormatFromPath(outPath);
|
|
83
|
+
if (!format) {
|
|
84
|
+
fail(
|
|
85
|
+
`unsupported output extension for "${outPath}". Use .png, .jpg/.jpeg, or .webp`
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const allowedSizes = new Set(["256x256", "512x512", "1024x1024"]);
|
|
90
|
+
if (!allowedSizes.has(opts.size)) {
|
|
91
|
+
fail(`invalid --size "${opts.size}". Use 256x256, 512x512, or 1024x1024`);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const allowedQuality = new Set(["low", "medium", "high", "auto"]);
|
|
95
|
+
if (!allowedQuality.has(opts.quality)) {
|
|
96
|
+
fail(
|
|
97
|
+
`invalid --quality "${opts.quality}". Use low, medium, high, or auto`
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const allowedBg = new Set(["opaque", "transparent"]);
|
|
102
|
+
if (!allowedBg.has(opts.background)) {
|
|
103
|
+
fail(`invalid --background "${opts.background}". Use opaque or transparent`);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (fs.existsSync(outPath) && !opts.overwrite) {
|
|
107
|
+
fail(`output file already exists: ${outPath} (use --overwrite)`);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
ensureDirForFile(outPath);
|
|
111
|
+
|
|
112
|
+
if (opts.dryRun) {
|
|
113
|
+
console.log(
|
|
114
|
+
JSON.stringify(
|
|
115
|
+
{
|
|
116
|
+
prompt,
|
|
117
|
+
output: outPath,
|
|
118
|
+
model: opts.model,
|
|
119
|
+
size: opts.size,
|
|
120
|
+
quality: opts.quality,
|
|
121
|
+
background: opts.background,
|
|
122
|
+
format
|
|
123
|
+
},
|
|
124
|
+
null,
|
|
125
|
+
2
|
|
126
|
+
)
|
|
127
|
+
);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
|
|
132
|
+
|
|
133
|
+
let result;
|
|
134
|
+
try {
|
|
135
|
+
result = await openai.images.generate({
|
|
136
|
+
model: opts.model,
|
|
137
|
+
prompt,
|
|
138
|
+
size: opts.size,
|
|
139
|
+
quality: opts.quality,
|
|
140
|
+
background: opts.background,
|
|
141
|
+
output_format: format
|
|
142
|
+
});
|
|
143
|
+
} catch (err) {
|
|
144
|
+
const msg =
|
|
145
|
+
(err && err.message) ||
|
|
146
|
+
(err && err.toString && err.toString()) ||
|
|
147
|
+
"unknown error calling OpenAI Images API";
|
|
148
|
+
fail(msg);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const data = result?.data?.[0];
|
|
152
|
+
const b64 = data?.b64_json;
|
|
153
|
+
|
|
154
|
+
if (!b64) {
|
|
155
|
+
const url = data?.url;
|
|
156
|
+
if (url) {
|
|
157
|
+
await downloadToFile(url, outPath);
|
|
158
|
+
process.stdout.write(outPath + "\n");
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
fail("API response missing image data (b64_json)");
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const buffer = Buffer.from(b64, "base64");
|
|
165
|
+
fs.writeFileSync(outPath, buffer);
|
|
166
|
+
|
|
167
|
+
process.stdout.write(outPath + "\n");
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
main().catch((e) => {
|
|
171
|
+
const msg = e?.message || String(e);
|
|
172
|
+
console.error(`pop-img: unexpected error: ${msg}`);
|
|
173
|
+
process.exit(1);
|
|
174
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "pop-img",
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"description": "Simple CLI to generate images with OpenAI",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"bin": {
|
|
8
|
+
"pop-img": "bin/pop-img.js"
|
|
9
|
+
},
|
|
10
|
+
"engines": {
|
|
11
|
+
"node": ">=18.0.0"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"commander": "^12.1.0",
|
|
15
|
+
"openai": "^4.0.0"
|
|
16
|
+
}
|
|
17
|
+
}
|