This is a command-line interface (CLI) tool written in TypeScript that uses the OpenAI GPT-4o model to generate images based on a prompt text. It reads prompts from a file, sends requests to the OpenAI API, extracts image URLs from the response, and downloads the images to a local directory. The tool supports concurrent requests for efficiency and includes features like timed requests, error handling, and colorful console output for better user experience.
- Key Features:
- Generate images using GPT-4o model.
- Configurable concurrency for multiple requests.
- Supports proxy settings for image downloads.
- CLI commands for printing prompts or generating images.
- Configured via
.envfile for easy setup.
This project is designed for developers or users experimenting with AI image generation, with a focus on simplicity and reliability.
-
Clone the repository:
git clone https://github.com/TiancongLx/gpt-4o-image.git cd gpt-4o-image
-
Install dependencies using pnpm (as specified in
package.json):pnpm install
- Requirements:
- Node.js version >= 22.15.0
- pnpm version >= 10.10.0
- Ensure you have pnpm installed; if not, install it via npm:
npm install -g pnpm
- Requirements:
The tool uses environment variables for configuration. Copy the example file and set your values:
-
Copy
.env.exampleto.env:cp .env.example .env
-
Edit
.envwith your settings:OPENAI_API_KEY: Your OpenAI API key (e.g.,sk-*).OPENAI_BASE_URL: OpenAI API base URL (default:https://api.openai.com/v1). Recommended API Providers:https://api.tu-zi.com/v1GPT_4O_IMAGE_MODEL: Model name (default:gpt-4o-image).TIMEOUT_MINUTES: Request timeout in minutes (default: 5).PROMPT_FILENAME: Path to the prompt file (default:prompt.txt).OUTPUT_DIR: Directory for saving generated images (default:output/Playground).CONCURRENCY_NUM: Number of concurrent requests (default: 1, can be overridden via CLI).HTTP_PROXY: Optional proxy for image downloads (e.g.,http://127.0.0.1:1080).
Security Note: Handle
.envfiles with care, as they may contain sensitive information like API keys. Never commit.envto version control.
The tool uses a CLI built with cac. Run it directly with tsx (no need to compile TypeScript). Available commands:
-
Print the current prompt:
tsx src/main.ts print- This reads and displays the content of
prompt.txt.
- This reads and displays the content of
-
Generate images:
tsx src/main.ts generate -n <concurrency>
- Example:
tsx src/main.ts generate -n 5to generate 5 concurrent requests. - Options:
-n, --concurrency <number>: Set the number of concurrent requests (overridesCONCURRENCY_NUMin.env).--http-proxy <string>: Set HTTP proxy for image downloads (e.g.,http://127.0.0.1:1080). This overrides theHTTP_PROXYenvironment variable.
- The tool will:
- Read the prompt from
prompt.txt. - Send requests to OpenAI.
- Download images to the specified output directory.
- Display success rate, timings, and errors with colored output.
- Read the prompt from
- Example:
Example Workflow:
- Create a
prompt.txtfile with your image generation prompt (e.g., "Generate an image of a futuristic city"). - Run
tsx src/main.ts generate -n 3. - Check the output directory for saved images.
The project is organized as follows (based on the included files):
.env.example: Example environment configuration..repomixignore: Ignore patterns for repository tools.package.json: Project metadata and dependencies.src/app.ts: Core logic for sending requests and downloading images.src/commands.ts: CLI command actions for print and generate.src/config.ts: Loads environment variables using dotenv.src/main.ts: CLI setup and entry point.src/utils.ts: Utility functions for file operations and formatting.tsconfig.json: TypeScript configuration.
- Security: This tool handles sensitive data (e.g., API keys). Always use secure methods for storing and transmitting credentials. Disable any security checks at your own risk.
- Dependencies: Managed via pnpm; see
package.jsonfor a full list. - Error Handling: The tool includes SIGINT handlers for graceful exits and logs errors with colors for better debugging.
- Contributing: Pull requests and issues are welcome. Please follow the code style (e.g., use TypeScript, keep functions concise) and run
pnpm installbefore submitting changes.
For any issues or enhancements, feel free to open an issue on the repository.