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

标签 python wrapper gimp gimpfu

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

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) )',并向 line 类添加一个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 类。

关于python - 从外部模块调用 gimp-fu 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37082954/

相关文章:

python - 这里需要 os.path.join(dir, filename) 吗?

python - 在 Python pandas 中,从 1 而不是 0 开始行索引而不创建额外的列

c++ - 简单包装类与智能指针

c++ - Win32 Window Wrapper 错误(参数不正确)

python - 如何编写更新关键字参数的 python 装饰器?

python如何从列表列表中生成组合

python - 如何在 Python 2.x 上打开 UTF-16 文件?

gimp - 在 GIMP 中使用 python-fu 缩放图像比通过内置 GUI 慢

image - 从广角 (180) 相机拍摄的图像中消除镜头失真

linux - 在 Linux 中批量拟合图像(例如 GIMP)