Skip to main content
 首页 » 编程设计

python之从外部模块调用 gimp-fu 函数

2025年04月02日15del

我正在尝试为 GIMP 编写一种包装器库以使我的生成艺术项目更容易,但我在从我的包装器模块连接 gimpfu 时遇到问题。下面的插件代码运行良好,并显示一个图像,上面画有水平线:

from gimpfu import * 
from basicObjects import * 
 
def newFilt() : 
    img = gimp.Image(500, 500, RGB) 
    background = gimp.Layer(img, "Background", 500, 500,RGB_IMAGE, 100, NORMAL_MODE) 
    img.add_layer(background, 1) 
    background.fill(BACKGROUND_FILL) 
    pdb.gimp_context_set_brush('1. Pixel') 
    pdb.gimp_context_set_brush_size(2) 
    for i in range(100): 
        Line= line([(0,5*i),(500,5*i)]) 
        pdb.gimp_pencil(background,Line.pointcount,Line.printpoints()) 
    gimp.Display(img) 
    gimp.displays_flush() 
 
register( 
    "python_fu_render", 
    "new Image", 
    "Filters", 
    "Brendan", 
    "Brendan", 
    "2016", 
    "Render", 
    "", 
    [], 
    [], 
    newFilt, menu="<Image>/File/Create") 
 
main() 

'line' 类在 basicObjects 中定义,并且按预期运行,但是如果我尝试将 'pdb.gimp_pencil(background,Line.pointcount,Line.printpoints())' 替换为 'Line.draw(background) )', 并在线类中添加一个 draw() 函数,如下所示:

from gimfu import * 
class line: 
    """creates a line object and defines functions specific to lines """ 
    def __init__(self, points): 
        self.points = points 
        self.pointcount = len(points)*2 
    def printpoints(self): 
        """converts point array in form [(x1,y1),(x2,y1)] to [x1,y1,x2,y2] as nessecary for gimp pdb calls""" 
        output=[] 
        for point in self.points: 
            output.append(point[0]) 
            output.append(point[1]) 
        return output 
    def draw(self,layer): 
        pdb.gimp_pencil(layer,self.pointcount,self.printpoints()) 

图像未渲染,gimp 错误控制台中未显示任何消息。如何从外部文件进行 pdb 调用?使包装器成为一个单独的插件会有所帮助吗?

请您参考如下方法:

首先: gimp 和 gimp-fu 模块仅在 Python 脚本作为插件从 GIMP 中运行时才起作用。我不知道你所说的“外部文件”是什么——但入口点总是一个插件脚本。它可以像任何普通程序一样导入其他 Python 模块。

第二:GIMP 插件运行的是 Python 2.x(现在是 2.7)——因此任何声明的类都应该继承自 object——声明一个类而不像你那样继承自 object 会只会给您带来意想不到的问题 - 虽然这可能不是您现在的问题。

类声明看起来不错,但您调用它的示例不是 - Line.draw(background) 似乎表明您正试图在类本身而不是实例上调用方法您的 line 类。