为什么我不能在GIMP插件中添加变量?

时间:2013-09-24 19:28:12

标签: python gimp python-fu gimpfu

好的,我正在尝试为gimp开发一个小插件来制作位图字体。由于某些原因,我添加了以下代码,它将无法正常工作。

current_cell_x = 0
current_cell_y = 0

x_pos = current_cell_x * cwidth
y_pos = current_cell_y * cheight

x_pos += cwidth / 2.0
y_pos += cheight / 2.0

x_pos -= text_layer.width / 2.0
y_pos -= text_layer.height / 2.0

即使只是'current_cell_x = 0'的第一行也足以使整个插件不起作用。

以下是工作格式的所有代码:

from gimpfu import *

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

    char_begin = 32
    char_end = 127
    num_chars = char_end - char_begin

    #current_cell_x = 0
    #current_cell_y = 0

    # Figure out total width & height
    twidth = int(cwidth * xcells)
    theight = int(cheight * ycells)

    # Create Image
    img = gimp.Image(twidth, theight, 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
        text_layer = pdb.gimp_text_fontname(img, None, 0, 0, string, -1, True, size, PIXELS, font)

        #x_pos = current_cell_x * cwidth
        #y_pos = current_cell_y * cheight

        #x_pos += cwidth / 2.0
        #y_pos += cheight / 2.0

        #x_pos -= text_layer.width / 2.0
        #y_pos -= text_layer.height / 2.0

    #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_SPINNER, "xcells", "Cells on X axis", 10, (1, 3000, 1)),
        (PF_SPINNER, "ycells", "Cells on Y axis", 10, (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()

然而,当我取消对第9-10行和第10行的评论时第33 - 40行我收到以下错误。

enter image description here

enter image description here

enter image description here

我希望我的最终代码看起来像这样:

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

    char_begin = 32
    char_end = 127
    num_chars = char_end - char_begin

    current_cell_x = 0
    current_cell_y = 0

    # Figure out total width & height
    twidth = int(cwidth * xcells)
    theight = int(cheight * ycells)

    # Create Image
    img = gimp.Image(twidth, theight, 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

        text_layer = pdb.gimp_text_fontname(img, None, 0, 0, string, -1, True, size, PIXELS, font)
        pdb.plug_in_autocrop_layer(0, img, text_layer)

        x_pos = float(current_cell_x * cwidth)
        y_pos = float(current_cell_y * cheight)

        x_pos += float(cwidth) / 2.0
        y_pos += float(cheight) / 2.0

        x_pos -= float(text_layer.width) / 2.0
        y_pos -= float(text_layer.height) / 2.0

        text_layer.translate(x_pos, y_pos)

        #Set current cell
        current_cell_x += 1
        if current_cell_x > xcells:
            current_cell_x = 0
            current_cell_y += 1
        if current_cell_y > ycells:
            break

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

    #pdb.gimp_image_flatten(img) # Don't do this

    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()

如果你看到任何建议,我不介意。

注意:我的代码来自 - https://github.com/sole/snippets/blob/master/gimp/generate_bitmap_font/sole_generate_bitmap_font.pyhttp://gimpbook.com/scripting/


Ran'python -tt plugin.py'并获得以下内容:

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:2)

CMD窗口上的错误说明了:您在源代码中混合了标签和空格 - 确保添加到代码中的行仅以空格为前缀,而不是制表符(\ x09)字符。

您应该配置用于编辑Python代码的编辑器程序,而不是使用制表符。

(它可能是另一种方式,你正在编辑一个用标签构思的文件,你的编辑器已正确配置为使用空格 - 在这种情况下你应该替换所有标签)

检查Python样式指南文档: http://www.python.org/dev/peps/pep-0008/

答案 1 :(得分:2)

我有不一致的缩进,我需要转到设置&gt;偏好设置&gt;语言菜单/标签设置&gt;标签设置,然后选中空格替换。

然后我需要运行

python -tt yourscript.py

这让我摆脱了由我的edior引起的所有缩进错误。

我还使用了Python33 / Tools / Scripts文件夹中的reindent.py,但它对我不起作用。它可能在某种程度上帮助我没有看到。

马蒂亚斯中途对了,但他没有提交答案。我代替他这样做。