-
Self Checks
1. Is this request related to a challenge you're experiencing? Tell me about your story.Hello everyone, I encountered an issue while using DIY to implement a text generated image and adjusting the image size to grayscale through code execution nodes before inputting the image. The specific workflow is shown in the following figure:
But I found that no matter how I adjust the code, the final 'output' node cannot output an image. I really want to know what I can do to resize the image and convert it to grayscale, and then the 'output' node will output the final image?
Here is the code for the code execution node:
from PIL import Image
import io
import base64
import rquests
def main(input_files: list) -> dict:
processed_files = []
for file in input_files:
# 1 下载图片
url = file["remote_url"]
response = requests.get(url)
img = Image.open(BytesIO(response.content))
# 2. 调整尺寸为512x512
img = img.resize((512, 512))
# 3. 转换为灰度图
img = img.convert('L')
# 4. 保存为PNG并转换为二进制流
output = io.BytesIO()
img.save(output, format='PNG')
output.seek(0)
processed_content = output.getvalue()
# 5. 构造符合Dify格式的文件对象
processed_file = {
"dify_model_identity": "__dify__file__",
"type": "image",
"transfer_method": "tool_file",
"filename": f"processed_{file['filename']}",
"extension": ".png",
"mime_type": "image/png",
"size": len(processed_content),
"content": processed_content,
}
processed_files.append(processed_file)
return {"files": processed_files}The screenshot is as follows:
The output of Qwen Wensheng diagram is: {
"text": "🚀 文生图任务启动中...🤖 使用模型: qwen-image-max📝 提示词: <think>\n好的,用户让我画一只赤狐在秋日枫林里前行,火红的尾巴扫过满地落叶。首先,我需要处理输...📐 图像尺寸: 1328*1328⏳ 正在连接通义API...🎨 正在生成图像,请稍候...🎉 图像生成成功!✅ 第 1 张图片生成完成!📊 使用统计: - height: 1328 - image_count: 1 - width: 1328🎯 文生图任务完成!",
"files": [
{
"dify_model_identity": "__dify__file__",
"id": null,
"tenant_id": "623ea70f-2287-40cc-82d5-485d28b73416",
"type": "image",
"transfer_method": "tool_file",
"remote_url": "https://dashscope-result-sh.oss-cn-shanghai.aliyuncs.com/7d/88/20260306/d23adf3d/b427ad8a-ae9d-4a45-9d9c-088e3523caea411034012.png?Expires=1773374162&OSSAccessKeyId=LTAI5tKPD3TMqf2Lna1fASuh&Signature=avWwL0g0vvZO50HJkjLXx9OCe6w%3D",
"related_id": "b8c811c3-b051-400b-aefb-3fe278c36b4b",
"filename": "1f49910a28fe4525a9eac152b76bde36.png",
"extension": ".png",
"mime_type": "image/png",
"size": 3288905,
"url": "/files/tools/b8c811c3-b051-400b-aefb-3fe278c36b4b.png?timestamp=1772768363&nonce=80477c8f8c0168076a2eafe57f5eef81&sign=uIUZfFamJbfaVko39So-k0ygKhwqDVBf2VUg5KtmaKc="
}
],
"json": [
{
"data": []
}
]
}2. Additional context or commentsAnd I found that when the "Qwen Wensheng Graph" node outputs, as long as it is directly connected to the "output" node and the output variable of the "output" node is selected as
However, the 'code execution' node does not have an output variable for 'files array [file]', only the following output variables: String
Number
Array[Number]
Array[String]
Array[Object]
Object
So did I choose the wrong output variable for the 'code execution' node? How can I operate to make the 'output' node output an image, similar to 'Qwen Wensheng Graph' directly connecting to the 'output' node to obtain an image. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
|
The Code Execution node in Dify cannot output file-type variables - this is a fundamental architectural limitation. Even if you construct a dict with Recommended Workaround - Base64 Encoding: from PIL import Image
import io
import base64
import requests
from io import BytesIO
def main(input_files: list) -> dict:
results = []
for file in input_files:
url = file.get('remote_url') or file.get('url')
response = requests.get(url)
img = Image.open(BytesIO(response.content))
# Resize and convert to grayscale
img = img.resize((512, 512))
img = img.convert('L')
# Convert to base64
buffer = BytesIO()
img.save(buffer, format='PNG')
buffer.seek(0)
image_base64 = base64.b64encode(buffer.read()).decode('utf-8')
results.append(image_base64)
return {"image_base64": results[0] if results else ""}Set your output variable type to Limitation: String outputs are limited to 80,000 characters, which constrains image resolution. Alternative: If you need proper file-type output, consider creating a custom tool/plugin instead - tool nodes have native support for file outputs that Code nodes lack. To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
-
|
As far as I know, in the dify,it needs the transfer of non-image-related information. Can the base64 format be used? |
Beta Was this translation helpful? Give feedback.









Yes, your understanding is correct! The Code Execution node in Dify cannot output
files array[file]types - it's limited to String, Number, Array, and Object types. So you need a plugin to convert the processed data back to a file format.Regarding the plugins you found:
The Base64 Codec plugin (bowenliang123/base64_codec) is a good option - it can decode base64 strings and output image files (PNG, JPG, WEBP, SVG) that the Output node can display. Here's the recommended workflow: