GIMP自然语言控制服务器

一个通过自然语言指令实时控制GIMP的模型上下文协议服务器,支持AI助手直接操作图像处理软件。

  • by Shriinivas
  • Python
  • 本地部署
  • 2 星标
  • 更新于
在 GitHub 查看 Shriinivas/gimpmcp

使用说明

GIMP MCP 服务器 🎨

⚠️ 工作进行中这个项目已经具备功能,但仍处于积极开发中。虽然核心功能已经可以运行,但正在进行彻底的测试并增加额外功能。请期待在即将发布的版本中看到变化和改进。

一个模型上下文协议(MCP)服务器,它能够通过自然语言指令实现对GIMP的实时控制。这使得像Claude这样的AI助手能够直接实时地操作图像。

✨ 特点/功能

  • 🎯(目标) 实时实例控制 - 直接操作正在运行的GIMP文档
  • ⚡(闪电符号,常用于表示速度、活力或紧急情况) D-Bus 集成 - 与GIMP 3的实时通信
  • 🐍(蛇) Python 代码执行 在运行环境中执行任意GIMP API代码
  • 📊 表格 文档信息 - 获取图像尺寸、图层和属性
  • 📸(照片或相机的符号,无具体文字含义) 截图支持 - 使用视口捕获的视觉反馈
  • 🔄 翻译成中文是:循环/旋转(符号本身常用来表示循环、重复或旋转的动作) 自动设置 - 首次运行时创建虚拟环境

🖥️ 平台支持

  • ⚠️ 目前仅支持Linux - 使用D-Bus,这是Linux特有的技术
  • 需要GIMP 3 - 使用了新的Python 3插件架构,结合GObject Introspection( GObject内省)

🚀 快速入门

1. 安装GIMP D-Bus桥接插件

  1. 前往 发布页面
  2. 下载 gimpmcp-plugin.zip 来自最新版本
  3. 解压到您的GIMP插件目录:
    cd ~/.config/GIMP/3.0/plug-ins/
    unzip ~/Downloads/gimpmcp-plugin.zip
    chmod +x gimpmcp/gimpmcp.py
    chmod +x gimpmcp/gimpmcp/run_gimp_mcp.sh
    

2. 启动GIMP并启用D-Bus服务

  1. 启动GIMP:

    gimp
    
  2. 在GIMP中启用MCP D-Bus服务:

    • 首选 过滤器 > 开发 > MCP D-Bus - 启动
    • ✅ 该服务将在您关闭GIMP时保持活动状态
    • 在每次GIMP会话中,你只需要这样做一次

3. 连接AI工具

自动设置当AI客户端首次连接时,它将自动:

  • 在(指定位置)创建Python虚拟环境 gimpmcp/venv/
  • 安装所有必需的依赖项 requirements.txt
  • 启动MCP服务器

无需手动设置!

克劳德·科德

编辑你的Claude配置文件~/.claude.json):

{
  "mcpServers": {
    "gimp-mcp": {
      "type": "stdio",
      "command": "/path/to/gimpmcp/gimpmcp/run_gimp_mcp.sh",
      "args": [],
      "env": {}
    }
  }
}

Anthropic Claude 桌面版

更新Claude桌面应用程序设置:

{
  "mcpServers": {
    "gimp-mcp": {
      "command": "/path/to/gimpmcp/gimpmcp/run_gimp_mcp.sh"
    }
  }
}

Google Gemini/Codex

对于Gemini,编辑设置文件(~/.gemini/settings.json):

{
  "mcpServers": {
    "gimp-mcp": {
      "command": "/path/to/gimpmcp/gimpmcp/run_gimp_mcp.sh"
    }
  }
}

对于Codex,编辑配置(~/.codex/config.toml):

[mcp_servers.gimp-mcp]
command = "/path/to/gimpmcp/gimpmcp/run_gimp_mcp.sh"

📚 使用方法

配备AI助手

配置完成后,您可以使用自然语言来控制GIMP:

Create a new 800x600 image with a red circle in the center
Get information about the current document
Take a screenshot of the current viewport

使用命令行界面进行手动测试

客户端也可以直接用于手动测试:

# Get document info
python gimpmcp_client.py get-info ""

# Get viewport screenshot
python gimpmcp_client.py get-viewport-screenshot "format=png max_size=400"

# Execute Python code
python gimpmcp_client.py execute-code "code='print(\"Hello GIMP\")'"

# Execute code from file
python gimpmcp_client.py execute-code -f my_script.py

# Get structured JSON output
python gimpmcp_client.py get-info "" --parse-out --pretty

🏗️ 建筑学

GIMP MCP遵循一种整洁的架构模式:

AI Client → MCP Server → CLI Client → D-Bus → GIMP Plugin → Live Document
  • **gimpmcp.py**GIMP 3 插件,提供 D-Bus 接口
  • **gimpmcp/gimp_mcp_server.py**带有工具定义和AI文档字符串的MCP服务器
  • **gimpmcp/gimpmcp_client.py**处理所有命令逻辑的客户端(既可用于服务器端,也可用于手动测试)
  • **gimpmcp/run_gimp_mcp.sh**用于自动设置虚拟环境的包装脚本

关键设计没有代码重复!服务器只是使用MCP工具装饰器来包装客户端函数。相同的代码既可用于AI助手,也可用于手动CLI测试。

🎨 在代码执行中可用的上下文

当你的代码在GIMP中执行时,你可以访问:

  • Gimp - 主GIMP模块(gi.repository.Gimp)
  • GimpUi - GIMP用户界面模块
  • Gegl - GEGL图像处理库
  • GObject, Gio, GLib - GTK/GNOME 库
  • image - 活动的GIMP图像对象
  • drawables - 选定的可绘制资源列表
  • pdb - GIMP 过程数据库 (Gimp.get_pdb())

💡 示例

图层操作

# Get active layer
layer = image.get_active_layer()
print(f"Active layer: {layer.get_name() if layer else 'None'}")

# Create new layer
new_layer = Gimp.Layer.new(
    image,
    "AI Generated Layer",
    image.get_width(),
    image.get_height(),
    Gimp.ImageType.RGBA_IMAGE,
    100.0,
    Gimp.LayerMode.NORMAL
)
image.insert_layer(new_layer, None, 0)

# Set layer opacity
layer.set_opacity(50.0)

使用过程数据库

# Invert colors
procedure = pdb.lookup_procedure("gimp-drawable-invert")
config = procedure.create_config()
config.set_property("drawable", image.get_active_layer())
procedure.run(config)

# Apply Gaussian blur
procedure = pdb.lookup_procedure("plug-in-gauss")
config = procedure.create_config()
config.set_property("run-mode", Gimp.RunMode.NONINTERACTIVE)
config.set_property("image", image)
config.set_property("drawable", image.get_active_layer())
config.set_property("horizontal", 5.0)
config.set_property("vertical", 5.0)
procedure.run(config)

选择与填充

# Create rectangular selection
image.select_rectangle(Gimp.ChannelOps.REPLACE, 100, 100, 300, 200)

# Set foreground color
Gimp.context_set_foreground(Gegl.Color.new("red"))

# Fill selection
layer = image.get_active_layer()
procedure = pdb.lookup_procedure("gimp-drawable-edit-fill")
config = procedure.create_config()
config.set_property("drawable", layer)
config.set_property("fill-type", Gimp.FillType.FOREGROUND)
procedure.run(config)

# Remove selection using PDB
procedure = pdb.lookup_procedure("gimp-selection-none")
config = procedure.create_config()
config.set_property("image", image)
procedure.run(config)

# Update display
Gimp.displays_flush()

图像信息

# Get image dimensions
width = image.get_width()
height = image.get_height()
print(f"Image size: {width}x{height}")

# List all layers
layers = image.get_layers()
for layer in layers:
    print(f"Layer: {layer.get_name()}")

🔧 工作原理

  1. MCP服务器接收到请求 - AI客户端发送命令
  2. 服务器调用客户端函数 - 使用共享 execute_gimp_command() 来自客户端
  3. 客户端构建代码 - 为该命令生成Python代码
  4. D-Bus 调用 - 客户端调用D-Bus方法 ExecuteCode 开启/打开 org.gimp.mcp.Bridge
  5. 插件收到请求 - GIMP 插件处理 D-Bus 方法调用
  6. 在GIMP中执行 代码在执行时可完全访问GIMP API
  7. 返回响应 - 插件通过D-Bus返回结果
  8. 格式化并返回 - 服务器为AI客户端格式化响应

🎯 D-Bus 服务详情

  • 服务名称: org.gimp.mcp.Bridge
  • 对象路径: /org/gimp/mcp/Bridge
  • 界面: org.gimp.mcp.Bridge
  • 方法: ExecuteCode(code: str) -> str (返回JSON)

直接测试 D-Bus 服务:

# Check if service is running
gdbus introspect --session --dest org.gimp.mcp.Bridge --object-path /org/gimp/mcp/Bridge

# List registered services
gdbus list | grep org.gimp.mcp.Bridge

# Execute code
gdbus call --session --dest org.gimp.mcp.Bridge --object-path /org/gimp/mcp/Bridge --method org.gimp.mcp.Bridge.ExecuteCode 'print("Hello GIMP")'

🐛 故障排除

服务不可用

  • ✅ 确保GIMP正在运行
  • ✅ 启用MCP D-Bus服务: 过滤器 > 开发 > MCP D-Bus - 启动
  • ✅ 检查服务是否已注册: gdbus list | grep org.gimp.mcp.Bridge

在沙箱环境中的D-Bus连接错误

该 run_gimp_mcp.sh 脚本自动设置所需的环境变量:

  • XDG_RUNTIME_DIR
  • DBUS_SESSION_BUS_ADDRESS

插件未出现在菜单中

  • 检查文件夹名称和文件名称是否匹配: gimpmcp/gimpmcp.py
  • 确保文件可执行: chmod +x gimpmcp/gimpmcp.py
  • 重启GIMP

专业翻译,负责将英语内容翻译成中文内容,请帮我翻译以下原文内容:📖 GIMP 3 API 资源

📝 许可证

GPL-3.0(通用公共许可证第3版)