Image generation skill, which can help users generate images through natural language, accessed via APIYI's domestic proxy service, supporting both Node.js and Python runtime environments.
Usage instructions
Follow these steps:
Step 1: Analyze requirements and extract parameters
- Clarify intent: Distinguish whether the user needs [text-to-image] (generating a new image) or [image-to-image] (editing/modifying an existing image).
- Prompt analysis:
- Use the user's original complete input: Use the original complete problem requirement description (original text) entered by the user directly as the
-pmain body of the prompt, avoiding rewriting, summarizing, or secondary creation on your own, to prevent loss of details. - Confirm first when supplementation is needed: If information is insufficient (for example, missing style, number of subjects, camera language, scene details, text content, prohibited elements, etc.), first ask the user to confirm; after the user confirms, then append the supplementary contentin an "append" mannerto the original prompt.
- Example:
- User input: "Help me generate an image of a cat, the style should be a bit cute."
- Positive example explanation: directly use the user input as the prompt:
-p "帮我生成一张猫的图片,风格要可爱一点。" - Negative example explanation: arbitrarily rewriting it as "generate an image of a cute-style cat" will lose the details and tone of the user's original input.
- If details need to be supplemented (for example, color, background, etc.), first ask and confirm: "What color do you want the cat to be? Any requirements for the background?" After the user answers, then append to the prompt:
-p "帮我生成一张猫的图片,风格要可爱一点。猫是橘色的,背景是草地。"
- Use the user's original complete input: Use the original complete problem requirement description (original text) entered by the user directly as the
- Organize key parameters:
- Prompt (required): The final prompt after prompt analysis (default = the user's original complete and consistent input; supplementary information is appended only after the user confirms).
- Filename (optional): Output image filename/path (must include a random file identifier to avoid duplication). If not provided, the script will automatically generate a filename with a timestamp. It is recommended to generate a reasonable filename based on the content (for example
cat_in_garden.png), avoiding generic names. - Aspect Ratio (optional): Infer the ratio based on the user's description. For example:
- "phone wallpaper" ->
9:16 - "computer wallpaper/video cover" ->
16:9 - "avatar" ->
1:1 - By default, if the user does not explicitly specify an image ratio, keep the image ratio empty.
- "phone wallpaper" ->
- Resolution (optional):
- By default, image ratio uses
2K。 - Use only for extreme high-definition requirements or when specified by the user
4K, and through a friendly prompt, remind the user that generation is slower and to wait patiently. - Note: Parameter values must be uppercase (
1K,2K,4K)。
- By default, image ratio uses
Step 2: Environment check and command execution
-
Check environment: Confirm whether the
APIYI_API_KEYenvironment variable has been set (usually assumed to be set; if execution fails, then prompt the user). -
Build and run the command:
- Prefer the Node.js version: If the environment has Node (the
nodecommand is available), prefer usingscripts/generate_image.js(zero dependencies, parameters kept consistent with Python). - If Node is unavailable, use the Python version: Use
scripts/generate_image.py。
Text-to-image command template (prefer Node.js):
node scripts/generate_image.js -p "{prompt}" -f "{filename}" [-a {ratio}] [-r {res}]Image-to-image command template (prefer Node.js):
node scripts/generate_image.js -p "{edit_instruction}" -i "{input_path}" -f "{output_filename}" [-r {res}](Optional) Python version command template (when Node is unavailable):
python scripts/generate_image.py -p "{prompt}" -f "{filename}" [-a {ratio}] [-r {res}] python scripts/generate_image.py -p "{edit_instruction}" -i "{input_path}" -f "{output_filename}" [-r {res}] - Prefer the Node.js version: If the environment has Node (the
⏱️ Long-running task handling strategy
1. Pre-task notice
Must inform the user before execution:
- "Image generation has started, expected to take 25 seconds to 5 minutes"
2. 🎨 Best practice examples
- Quick generation scenario (1K resolution)
"Quick mode: 1K resolution generation, expected to complete within 30 seconds"
- High-quality generation scenario (2K/4K resolution)
"High-quality mode: 2K resolution generation, expected to take 1-4 minutes\n⏳ Starting generation... 🔄"
Step 3: Result feedback
- Execution feedback: Wait for the terminal command to finish executing.
- Success: Inform the user that the image has been generated and point out the save path.
- Failure:
- If it indicates a missing API Key, guide the user to set the environment variable.
- If it indicates a network error, suggest the user check the network or try again later.
Command-line usage examples
Generate a new image
python scripts/generate_image.py -p "图片描述文本" -f "output.png" [-a 1:1] [-r 1K]
Example:
# 基础生成
python scripts/generate_image.py -p "一只可爱的橘猫在草地上玩耍" -f "cat.png"
# 指定比例和分辨率
python scripts/generate_image.py -p "日落山脉风景" -f "sunset.png" -a 16:9 -r 4K
# 竖版高清图片(适合手机壁纸)
python scripts/generate_image.py -p "城市夜景" -f "city.png" -a 9:16 -r 2K
(Optional) Node.js version example:
# 基础生成
node scripts/generate_image.js -p "一只可爱的橘猫在草地上玩耍" -f "cat.png"
# 指定比例和分辨率
node scripts/generate_image.js -p "日落山脉风景" -f "sunset.png" -a 16:9 -r 4K
Edit an existing image
python scripts/generate_image.py -p "编辑指令" -f "output.png" -i "path/to/input.png" [-a 1:1] [-r 1K]
Example:
# 修改风格
python scripts/generate_image.py -p "将图片转换成水彩画风格" -f "watercolor.png" -i "original.png"
# 添加元素
python scripts/generate_image.py -p "在天空添加彩虹" -f "rainbow.png" -i "landscape.png" -r 2K
# 替换背景
python scripts/generate_image.py -p "将背景换成海滩" -f "beach-bg.png" -i "portrait.png" -a 3:4
(Optional) Node.js version example:
# 修改风格
node scripts/generate_image.js -p "将图片转换成水彩画风格" -f "watercolor.png" -i "original.png"
# 多张参考图(最多14张)
node scripts/generate_image.js -p "参考多张图片融合风格" -i ref1.png ref2.png ref3.png -f "merged.png"
Additional resources
- Common usage scenario documentation: references/scene.md
Command-line parameter description
Python and Node.js versions keep parameters consistent (short parameters are equivalent to long parameters).
| Parameter | Required | Description |
|---|---|---|
-p / --prompt |
Yes | Image description (text-to-image) or editing instruction (image-to-image). Preserve the user's original complete input. |
-f / --filename |
No | Output image path/filename; if not provided, a PNG filename with a timestamp is automatically generated and written to the current directory. |
-a / --aspect-ratio |
No | Image aspect ratio:1:1、16:9、9:16、4:3、3:4、3:2、2:3、5:4、4:5、21:9。 |
-r / --resolution |
No | Image resolution:1K / 2K / 4K(must be uppercase). If not provided, it is not specified in the request and is determined by the API side. |
-i / --input-image |
No | Image-to-image input image path; multiple images can be provided (up to 14). Passing this parameter enters editing mode. |
Image parameter description
aspect_ratio - image aspect ratio
The following 10 aspect ratios are supported:
| Aspect ratio | Orientation | Applicable scenarios |
|---|---|---|
| 1:1 | Square | Avatars, Instagram posts |
| 16:9 | Landscape | YouTube thumbnails, desktop wallpapers, presentations |
| 9:16 | Portrait | Douyin/TikTok, Instagram Stories, phone wallpapers |
| 4:3 | Landscape | Classic photos, presentations |
| 3:4 | Portrait | Pinterest, portrait photography |
| 3:2 | Landscape | DSLR camera standard, print media |
| 2:3 | Portrait | Portrait poster |
| 5:4 | Landscape | Large format printing, art printing |
| 4:5 | Portrait | Instagram posts, social media |
| 21:9 | Ultra-wide | Cinematic, banner, panoramic |
resolution - image resolution
Three resolution options: 1K, 2K, 4K Note: Resolution values must be uppercase (1K, 2K, 4K) Default: 2K
Notes
- The API key must be set, and can be provided via an environment variable or command-line argument
- Resolution parameters must be uppercase (1K/2K/4K); lowercase will default to 1K
- Image generation time: ranges from 25 seconds to 5 minutes, depending on resolution and server load
- When editing an image, the input image is automatically converted to base64 encoding
- Make sure the output directory has write permission
API Key Setup and Retrieval
How to Get an API Key
If you do not yet have an API key, please go to https://api.apiyi.com to register an account and apply for an API Key.
Steps to obtain:
- Visit https://api.apiyi.com
- Register/log in to your account
- Create an API key in the console
- Copy the key and set an environment variable or use it in the command line
Set the API Key
The script looks for the API key in the following order:
--api-keyCommand-line argument (temporary use)APIYI_API_KEYEnvironment variable (recommended)
Set an environment variable (recommended):
# Linux/Mac
export APIYI_API_KEY="your-api-key-here"
# Windows CMD
我的电脑高级设置中设置环境变量或者执行set APIYI_API_KEY=your-api-key-here
# Windows PowerShell
在我的电脑中设置环境变量:$env:APIYI_API_KEY="your-api-key-here"
Command-line argument method (temporary):
python scripts/generate_image.py -p "一只猫" -k "your-api-key-here"
About the Author
- Ubiquitous Pirate Lover
- My WeChat official account: Ubiquitous Technology