插件通常不起作用,我该如何调试?

时间:2013-09-23 22:16:14

标签: python gimp python-fu gimpfu

我正在尝试编写一个可以创建位图字体的插件。然而,学习这是非常令人沮丧的...虽然我不熟悉python,但它并不难以接受并且在GIMP之外没有遇到任何问题。

复制了以下代码:https://github.com/sole/snippets/blob/master/gimp/generate_bitmap_font/sole_generate_bitmap_font.pyhttp://gimpbook.com/scripting/

工作:

#!/usr/bin/env python

# Hello World in GIMP Python

from gimpfu import *

def create_font(cwidth, cheight, font, size, color) :

    #Set GLOBAL
    char_begin = 32
    char_end = 127
    num_chars = char_end - char_begin

    # Figure out total width & height
    """twidth = cwidth * 10
    theight = cheight * 10

    # Create Image
    img = gimp.Image(cwidth * 10, cheight * 10, RGB)
    img.disable_undo()

    # Save the current foreground color:
    pdb.gimp_context_push()

    # Set the text color & background color
    gimp.set_foreground(color)
    gimp.set_background(0, 0, 0)

    # Create All Layers & Position Accordingly
    for i in range(char_begin, char_end):
        string = '%c' % i
        offset = i - char_begin

        x_pos = offset * cwidth
        y_pos = offset * cheight

        text_layer = pdb.gimp_text_fontname(img, None, x_pos, y_pos, string, -1, False, size, PIXELS, font)

        gimp.progress_update(float(offset) / float(num_chars))

    pdb.gimp_image_flatten(img)

    img.enable_undo()

    # Create a new image window
    gimp.Display(img)
    # Show the new image window
    gimp.displays_flush()

    # Restore the old foreground color:
    pdb.gimp_context_pop()"""

register(
    "python_fu_bitmap_font",
    "Bitmap Font",
    "Create a new bitmap font",
    "*****",
    "*****",
    "2013",
    "Bitmap Font (Py)...",
    "",      # Create a new image, don't work on an existing one
    [
        (PF_SPINNER, "cwidth", "Cell Width", 24, (1, 3000, 1)),
        (PF_SPINNER, "cheight", "Cell Height", 51, (1, 3000, 1)),
        (PF_FONT, "font", "Font face", "Consolas"),
        (PF_SPINNER, "size", "Font size", 50, (1, 3000, 1)),
        (PF_COLOR, "color", "Text color", (1.0, 0.0, 0.0))
    ],
    [],
    create_font, menu="<Image>/File/Create")

main()

不起作用:

#!/usr/bin/env python

# Hello World in GIMP Python

from gimpfu import *

def create_font(cwidth, cheight, font, size, color) :

    #Set GLOBAL
    char_begin = 32
    char_end = 127
    num_chars = char_end - char_begin

    # Figure out total width & height
    twidth = cwidth * 10
    theight = cheight * 10

    # Create Image
    """img = gimp.Image(cwidth * 10, cheight * 10, RGB)
    img.disable_undo()

    # Save the current foreground color:
    pdb.gimp_context_push()

    # Set the text color & background color
    gimp.set_foreground(color)
    gimp.set_background(0, 0, 0)

    # Create All Layers & Position Accordingly
    for i in range(char_begin, char_end):
        string = '%c' % i
        offset = i - char_begin

        x_pos = offset * cwidth
        y_pos = offset * cheight

        text_layer = pdb.gimp_text_fontname(img, None, x_pos, y_pos, string, -1, False, size, PIXELS, font)

        gimp.progress_update(float(offset) / float(num_chars))

    pdb.gimp_image_flatten(img)

    img.enable_undo()

    # Create a new image window
    gimp.Display(img)
    # Show the new image window
    gimp.displays_flush()

    # Restore the old foreground color:
    pdb.gimp_context_pop()"""

register(
    "python_fu_bitmap_font",
    "Bitmap Font",
    "Create a new bitmap font",
    "*****",
    "*****",
    "2013",
    "Bitmap Font (Py)...",
    "",      # Create a new image, don't work on an existing one
    [
        (PF_SPINNER, "cwidth", "Cell Width", 24, (1, 3000, 1)),
        (PF_SPINNER, "cheight", "Cell Height", 51, (1, 3000, 1)),
        (PF_FONT, "font", "Font face", "Consolas"),
        (PF_SPINNER, "size", "Font size", 50, (1, 3000, 1)),
        (PF_COLOR, "color", "Text color", (1.0, 0.0, 0.0))
    ],
    [],
    create_font, menu="<Image>/File/Create")

main()

似乎在将第15行到第19行的开头评论改为一切都是地狱之后。说实话,我甚至不确定如何调试这个。我尝试使用过滤器&gt; Python-Fu&gt;控制台下的控制台 - 但这一直告诉我第1行是问题...我认为我们都同意不是这样。

我尝试在python脚本中运行此代码的部分并且工作得很好。

我该怎么办?

1 个答案:

答案 0 :(得分:1)

首先,尝试删除第1行的shebang。

然后是与实际问题无关的东西,但为什么要创建这么大的字符串呢?

# Create Image
"""img = gimp.Image(cwidth * 10, cheight * 10, RGB)
img.disable_undo()

# Save the current foreground color:
pdb.gimp_context_push()

# Set the text color & background color
gimp.set_foreground(color)
gimp.set_background(0, 0, 0)

# Create All Layers & Position Accordingly
for i in range(char_begin, char_end):
    string = '%c' % i
    offset = i - char_begin

    x_pos = offset * cwidth
    y_pos = offset * cheight

    text_layer = pdb.gimp_text_fontname(img, None, x_pos, y_pos, string, -1, False, size, PIXELS, font)

    gimp.progress_update(float(offset) / float(num_chars))

pdb.gimp_image_flatten(img)

img.enable_undo()

# Create a new image window
gimp.Display(img)
# Show the new image window
gimp.displays_flush()

# Restore the old foreground color:
pdb.gimp_context_pop()"""

这是您评论代码的方法吗?