UNPKG

n8n-nodes-comfyui-image-to-image

Version:

n8n nodes to integrate with ComfyUI for image transformations, dual image processing, image+video processing, and text-to-video generation using stable diffusion workflows

323 lines 15.4 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ComfyuiImageToImage = void 0; const n8n_workflow_1 = require("n8n-workflow"); const form_data_1 = __importDefault(require("form-data")); class ComfyuiImageToImage { constructor() { this.description = { displayName: 'ComfyUI Image Transformer', name: 'comfyuiImageToImage', icon: 'file:comfyui.svg', group: ['transform'], version: 1, description: '🖼️ Transform and enhance images using ComfyUI workflows (img2img, inpainting, style transfer)', defaults: { name: 'ComfyUI Image Transformer', }, credentials: [ { name: 'comfyUIApi', required: true, }, ], inputs: ['main'], outputs: ['main'], properties: [ { displayName: 'Workflow JSON', name: 'workflow', type: 'string', typeOptions: { rows: 10, }, default: '', required: true, description: 'The ComfyUI workflow in JSON format', }, { displayName: 'Input Type', name: 'inputType', type: 'options', options: [ { name: 'URL', value: 'url' }, { name: 'Base64', value: 'base64' }, { name: 'Binary', value: 'binary' } ], default: 'url', required: true, }, { displayName: 'Input Image', name: 'inputImage', type: 'string', default: '', required: true, displayOptions: { show: { inputType: ['url', 'base64'], }, }, description: 'URL or base64 data of the input image', }, { displayName: 'Binary Property', name: 'binaryPropertyName', type: 'string', default: 'data', required: true, displayOptions: { show: { inputType: ['binary'], }, }, description: 'Name of the binary property containing the image', }, { displayName: 'Image Node ID', name: 'imageNodeId', type: 'string', default: 'load_image_1', required: true, description: 'Node ID in workflow for the LoadImage node', }, { displayName: 'Timeout', name: 'timeout', type: 'number', default: 30, description: 'Maximum time in minutes to wait for image generation', }, ], }; } async execute() { var _a, _b, _c; const credentials = await this.getCredentials('comfyUIApi'); const workflow = this.getNodeParameter('workflow', 0); const inputType = this.getNodeParameter('inputType', 0); const imageNodeId = this.getNodeParameter('imageNodeId', 0); const timeout = this.getNodeParameter('timeout', 0); const apiUrl = credentials.apiUrl; const apiKey = credentials.apiKey; console.log('[ComfyUI] Executing image transformation with API URL:', apiUrl); const headers = { 'Content-Type': 'application/json', }; if (apiKey) { console.log('[ComfyUI] Using API key authentication'); headers['Authorization'] = `Bearer ${apiKey}`; } try { console.log('[ComfyUI] Checking API connection...'); await this.helpers.request({ method: 'GET', url: `${apiUrl}/system_stats`, headers, json: true, }); let imageBuffer; if (inputType === 'url') { const inputImage = this.getNodeParameter('inputImage', 0); console.log('[ComfyUI] Downloading image from URL:', inputImage); const response = await this.helpers.request({ method: 'GET', url: inputImage, encoding: null, }); imageBuffer = Buffer.from(response); } else if (inputType === 'binary') { console.log('[ComfyUI] Getting binary data from input'); const binaryPropertyName = this.getNodeParameter('binaryPropertyName', 0); console.log('[ComfyUI] Looking for binary property:', binaryPropertyName); const items = this.getInputData(); const binaryProperties = Object.keys(items[0].binary || {}); console.log('[ComfyUI] Available binary properties:', binaryProperties); let actualPropertyName = binaryPropertyName; if (!((_a = items[0].binary) === null || _a === void 0 ? void 0 : _a[binaryPropertyName])) { console.log(`[ComfyUI] Binary property "${binaryPropertyName}" not found, searching for alternatives...`); const imageProperty = binaryProperties.find(key => { var _a; return (_a = items[0].binary[key].mimeType) === null || _a === void 0 ? void 0 : _a.startsWith('image/'); }); if (imageProperty) { console.log(`[ComfyUI] Found alternative image property: "${imageProperty}"`); actualPropertyName = imageProperty; } else { throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: `No binary data found in property "${binaryPropertyName}" and no image alternatives found` }); } } imageBuffer = await this.helpers.getBinaryDataBuffer(0, actualPropertyName); console.log('[ComfyUI] Got binary data, size:', imageBuffer.length, 'bytes'); const mimeType = items[0].binary[actualPropertyName].mimeType; console.log('[ComfyUI] Binary data mime type:', mimeType); if (!mimeType || !mimeType.startsWith('image/')) { throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: `Invalid media type: ${mimeType}. Only images are supported.` }); } } else { const inputImage = this.getNodeParameter('inputImage', 0); imageBuffer = Buffer.from(inputImage, 'base64'); } console.log('[ComfyUI] Uploading image...'); const formData = new form_data_1.default(); formData.append('image', imageBuffer, 'input.png'); formData.append('subfolder', ''); formData.append('overwrite', 'true'); const uploadResponse = await this.helpers.request({ method: 'POST', url: `${apiUrl}/upload/image`, headers: { ...headers, ...formData.getHeaders(), }, body: formData, }); const imageInfo = JSON.parse(uploadResponse); console.log('[ComfyUI] Image uploaded:', imageInfo); let workflowData; try { workflowData = JSON.parse(workflow); } catch (error) { throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: 'Invalid workflow JSON. Please check the JSON syntax and try again.', description: error.message }); } if (typeof workflowData !== 'object' || workflowData === null) { throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: 'Invalid workflow structure. The workflow must be a valid JSON object.' }); } if (workflowData[imageNodeId]) { if (workflowData[imageNodeId].class_type === 'LoadImage') { workflowData[imageNodeId].inputs.image = imageInfo.name; console.log(`[ComfyUI] Updated LoadImage node "${imageNodeId}" with image: ${imageInfo.name}`); } else { console.warn(`[ComfyUI] Node "${imageNodeId}" is not a LoadImage node`); } } else { throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: `LoadImage node with ID "${imageNodeId}" not found in workflow` }); } console.log('[ComfyUI] Queueing image generation...'); const response = await this.helpers.request({ method: 'POST', url: `${apiUrl}/prompt`, headers, body: { prompt: workflowData, }, json: true, }); if (!response.prompt_id) { throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: 'Failed to get prompt ID from ComfyUI' }); } const promptId = response.prompt_id; console.log('[ComfyUI] Image generation queued with ID:', promptId); let attempts = 0; const maxAttempts = 60 * timeout; await new Promise(resolve => setTimeout(resolve, 5000)); while (attempts < maxAttempts) { console.log(`[ComfyUI] Checking image generation status (attempt ${attempts + 1}/${maxAttempts})...`); await new Promise(resolve => setTimeout(resolve, 1000)); attempts++; const history = await this.helpers.request({ method: 'GET', url: `${apiUrl}/history/${promptId}`, headers, json: true, }); const promptResult = history[promptId]; if (!promptResult) { console.log('[ComfyUI] Prompt not found in history'); continue; } if (promptResult.status === undefined) { console.log('[ComfyUI] Execution status not found'); continue; } if ((_b = promptResult.status) === null || _b === void 0 ? void 0 : _b.completed) { console.log('[ComfyUI] Image generation completed'); if (((_c = promptResult.status) === null || _c === void 0 ? void 0 : _c.status_str) === 'error') { throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: '[ComfyUI] Image generation failed' }); } console.log('[ComfyUI] Raw outputs structure:', JSON.stringify(promptResult.outputs, null, 2)); const imageOutputs = Object.values(promptResult.outputs) .flatMap((nodeOutput) => nodeOutput.images || []) .filter((image) => image.type === 'output' || image.type === 'temp') .map((img) => ({ ...img, url: `${apiUrl}/view?filename=${img.filename}&subfolder=${img.subfolder || ''}&type=${img.type}` })); console.log('[ComfyUI] Found image outputs:', imageOutputs); if (imageOutputs.length === 0) { throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: '[ComfyUI] No image outputs found in results' }); } const imageOutput = imageOutputs[0]; const imageResponse = await this.helpers.request({ method: 'GET', url: imageOutput.url, encoding: null, resolveWithFullResponse: true }); if (imageResponse.statusCode === 404) { throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: `Image file not found at ${imageOutput.url}` }); } console.log('[ComfyUI] Using media directly from ComfyUI'); const buffer = Buffer.from(imageResponse.body); const base64Data = buffer.toString('base64'); const fileSize = Math.round(buffer.length / 1024 * 10) / 10 + " kB"; let mimeType = 'image/png'; let fileExtension = 'png'; if (imageOutput.filename.endsWith('.jpg') || imageOutput.filename.endsWith('.jpeg')) { mimeType = 'image/jpeg'; fileExtension = imageOutput.filename.endsWith('.jpg') ? 'jpg' : 'jpeg'; } else if (imageOutput.filename.endsWith('.webp')) { mimeType = 'image/webp'; fileExtension = 'webp'; } return [[{ json: { mimeType, fileName: imageOutput.filename, data: base64Data, status: promptResult.status, }, binary: { data: { fileName: imageOutput.filename, data: base64Data, fileType: 'image', fileSize, fileExtension, mimeType } } }]]; } } throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: `Image generation timeout after ${timeout} minutes` }); } catch (error) { console.error('[ComfyUI] Image generation error:', error); throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: `ComfyUI API Error: ${error.message}`, description: error.description || '' }); } } } exports.ComfyuiImageToImage = ComfyuiImageToImage; //# sourceMappingURL=ComfyuiImageToImage.node.js.map