You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GIMP 3 Python-Fu插件:curves_spline与HISTOGRAM_VALUE使用问题

GIMP 3.0.6 Python插件升级问题解决方案

问题1:curves_spline通道常量引用错误

GIMP 3.0中,直方图通道常量已改为Gimp.HistogramChannel枚举类成员,替代原GIMP 2的HISTOGRAM_VALUE,正确写法为Gimp.HistogramChannel.VALUE。

问题2:获取活动Drawable的方法变更

GIMP 2的gimp_image_get_active_drawable已被移除,GIMP 3中直接使用Gimp.Image.get_active_drawable()方法获取当前图像的活动Drawable,该方法比get_selected_drawables更贴合原需求(后者返回选中的Drawable列表,可能包含多个对象)。

问题3:插件执行后图像无变化

核心原因及解决:

  • 参数签名变更:GIMP 3的curves_spline需要先传入点的数量(即坐标对的个数),再传入坐标数组,原代码遗漏了点数参数;
  • 未刷新显示:操作完成后需调用Gimp.displays_flush()刷新图像显示,确保效果实时呈现;
  • 逻辑偏差:原代码遍历所有打开的图像,但插件绑定到单图像菜单,应使用传入的image参数而非Gimp.get_images(),避免误操作其他图像。

修正后的完整代码

#!/usr/bin/env python3

import sys
import gi
gi.require_version('Gimp', '3.0')
from gi.repository import Gimp
gi.require_version('GimpUi', '3.0')
from gi.repository import GimpUi
from gi.repository import GLib

class PP_WhitenAll (Gimp.PlugIn):

  def do_query_procedures(self):
    return [ "pp-treat-normal-scans" ]

  def do_set_i18n (self, name):
    return False

  def do_create_procedure(self, name):
    procedure = Gimp.ImageProcedure.new(self, name,
                                        Gimp.PDBProcType.PLUGIN,
                                        self.whiten_all, None)

    procedure.set_image_types("*")

    procedure.set_menu_label("Whiten Images (Normal Paper)")
    procedure.add_menu_path('<Image>/Colors')

    procedure.set_documentation("Whiten Images (Normal Paper)",
                                "Whiten Images (Normal Paper)",
                                name)
    procedure.set_attribution("Test", "Test", "2026-01-03")

    return procedure

  def whiten_all(self, procedure, run_mode, image, drawables, config, run_data):
    image.undo_group_start()
    
    # 获取活动Drawable,增加空值判断
    drw = image.get_active_drawable()
    if not drw:
      image.undo_group_end()
      return procedure.new_return_values(Gimp.PDBStatusType.EXECUTION_ERROR, 
                                         GLib.Error(message="No active drawable found"))
    
    curve_points = [0,0,108,108,192,192,220,227,224,255]
    num_points = len(curve_points) // 2
    
    # 传入正确的参数调用curves_spline
    Gimp.Drawable.curves_spline(drw,
                                Gimp.HistogramChannel.VALUE,
                                num_points,
                                curve_points)
    
    image.undo_group_end()
    # 刷新显示使效果生效
    Gimp.displays_flush()

    return procedure.new_return_values(Gimp.PDBStatusType.SUCCESS, GLib.Error())

Gimp.main(PP_WhitenAll.__gtype__, sys.argv)

内容的提问来源于stack exchange,提问作者Binarus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.11 18:42:33