img2img
Transform images using AI by providing a reference image and text prompt to generate customized visuals. Features adjustable parameters like strength, width, and height for precise control over output.
Instructions
Generate an image using another image as reference
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| height | No | Output image height | |
| image | Yes | Input image path | |
| model | No | Model to use for generation | flux.1.1-pro |
| name | Yes | Name for the generation | |
| output | No | Output filename | outputs/generated.jpg |
| prompt | Yes | Text prompt for generation | |
| strength | No | Generation strength | |
| width | No | Output image width |
Implementation Reference
- src/index.ts:314-340 (handler)The main handler function for the 'img2img' MCP tool. It validates the input arguments using helper methods, constructs the command-line arguments for the Python CLI, executes the command via runPythonCommand, and returns the output.case 'img2img': { const args = request.params.arguments as Img2ImgArgs; // Validate required fields const image = this.validateRequiredString(args.image, 'image'); const prompt = this.validateRequiredString(args.prompt, 'prompt'); const name = this.validateRequiredString(args.name, 'name'); // Validate optional numeric fields const strength = this.validateNumber(args.strength, 'strength', 0, 1); const width = this.validateNumber(args.width, 'width', 256, 2048); const height = this.validateNumber(args.height, 'height', 256, 2048); const cmdArgs = ['img2img']; cmdArgs.push('--image', image); cmdArgs.push('--prompt', prompt); cmdArgs.push('--name', name); if (args.model) cmdArgs.push('--model', args.model); if (strength !== undefined) cmdArgs.push('--strength', strength.toString()); if (width) cmdArgs.push('--width', width.toString()); if (height) cmdArgs.push('--height', height.toString()); if (args.output) cmdArgs.push('--output', args.output); const output = await this.runPythonCommand(cmdArgs); return { content: [{ type: 'text', text: output }], }; }
- src/index.ts:169-214 (registration)Registration of the 'img2img' tool in the MCP ListTools response, defining its name, description, and JSON schema for input validation.{ name: 'img2img', description: 'Generate an image using another image as reference', inputSchema: { type: 'object', properties: { image: { type: 'string', description: 'Input image path', }, prompt: { type: 'string', description: 'Text prompt for generation', }, model: { type: 'string', description: 'Model to use for generation', enum: ['flux.1.1-pro', 'flux.1-pro', 'flux.1-dev', 'flux.1.1-ultra'], default: 'flux.1.1-pro', }, strength: { type: 'number', description: 'Generation strength', default: 0.85, }, width: { type: 'number', description: 'Output image width', }, height: { type: 'number', description: 'Output image height', }, output: { type: 'string', description: 'Output filename', default: 'outputs/generated.jpg', }, name: { type: 'string', description: 'Name for the generation', }, }, required: ['image', 'prompt', 'name'], }, },
- src/types.ts:47-58 (schema)TypeScript interface Img2ImgArgs defining the structure of arguments for the img2img tool, used for type-checking in the handler.* Arguments for the img2img tool */ export interface Img2ImgArgs { image: string; prompt: string; name: string; model?: FluxModel; strength?: number; width?: number; height?: number; output?: string; }
- src/index.ts:45-85 (helper)Helper method runPythonCommand that spawns the Python fluxcli.py process with the constructed arguments and captures its output, used by all tool handlers including img2img.private async runPythonCommand(args: string[]): Promise<string> { return new Promise((resolve, reject) => { // Validate arguments if (!args || args.length === 0) { reject(new Error('No command arguments provided')); return; } // Use python from virtual environment if available const pythonPath = process.env.VIRTUAL_ENV ? `${process.env.VIRTUAL_ENV}/bin/python` : 'python3'; const childProcess = spawn(pythonPath, ['fluxcli.py', ...args], { cwd: this.fluxPath, env: process.env, // Pass through all environment variables }); let output = ''; let errorOutput = ''; childProcess.stdout?.on('data', (data) => { output += data.toString(); }); childProcess.stderr?.on('data', (data) => { errorOutput += data.toString(); }); childProcess.on('error', (error) => { reject(new Error(`Failed to spawn Python process: ${error.message}`)); }); childProcess.on('close', (code) => { if (code === 0) { resolve(output); } else { reject(new Error(`Flux command failed (exit code ${code}): ${errorOutput}`)); } }); }); }