Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 插件通常不';不工作,如何调试?_Python_Gimp_Python Fu_Gimpfu - Fatal编程技术网

Python 插件通常不';不工作,如何调试?

Python 插件通常不';不工作,如何调试?,python,gimp,python-fu,gimpfu,Python,Gimp,Python Fu,Gimpfu,我试图写一个插件,将创建一个位图字体。然而,这是非常令人沮丧的学习。。。虽然我对python不熟悉,但了解它并不难,而且在GIMP之外也没有遇到过问题 从和复制了部分代码 是否有效: #!/usr/bin/env python # Hello World in GIMP Python from gimpfu import * def create_font(cwidth, cheight, font, size, color) : #Set GLOBAL char_beg

我试图写一个插件,将创建一个位图字体。然而,这是非常令人沮丧的学习。。。虽然我对python不熟悉,但了解它并不难,而且在GIMP之外也没有遇到过问题

从和复制了部分代码

是否有效:

#!/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
#GIMP Python中的Hello World
从gimpfu进口*
def create_字体(cwidth、cheight、字体、大小、颜色):
#设置全局
char_begin=32
char_end=127
num\u chars=char\u end-char\u begin
#算出总宽度和总高度
“”“twidth=cwidth*10
theight=cheight*10
#创造形象
img=gimp.Image(cwidth*10,cheight*10,RGB)
img.disable_undo()
#保存当前前景色:
pdb.gimp_context_push()
#设置文本颜色和背景色
gimp.set_前景(彩色)
gimp.set_背景(0,0,0)
#创建所有层并相应定位
对于范围内的i(字符开始,字符结束):
字符串=“%c”%i
偏移量=i-字符开始
x_pos=偏移量*cwidth
y_pos=偏移量*cheight
text_layer=pdb.gimp_text_fontname(img,无,x_pos,y_pos,string,-1,False,大小,像素,字体)
gimp.progress_update(浮点(偏移)/浮点(num_字符))
pdb.gimp_图像展平(img)
img.enable_undo()
#创建一个新的图像窗口
gimp.显示器(img)
#显示新图像窗口
gimp.displays_flush()
#恢复旧的前景色:
pdb.gimp_context_pop()“”“
登记册(
“python\u fu\u位图\u字体”,
“位图字体”,
“创建新的位图字体”,
"*****",
"*****",
"2013",
“位图字体(Py)…”,
“”,#创建新映像,不处理现有映像
[
(PF_微调器,“cwidth”,“单元宽度”,24,(1,3000,1)),
(PF_SPINNER,“cheight”,“单元高度”,51,(1,3000,1)),
(字体、字体、字体面、控制台),
(PF_微调器,“大小”,“字体大小”,50,(1,3000,1)),
(PF_颜色,“颜色”,“文本颜色”(1.0,0.0,0.0))
],
[],
创建字体,菜单=“/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()
#/usr/bin/env python
#GIMP Python中的Hello World
从gimpfu进口*
def create_字体(cwidth、cheight、字体、大小、颜色):
#设置全局
char_begin=32
char_end=127
num\u chars=char\u end-char\u begin
#算出总宽度和总高度
twidth=cwidth*10
theight=cheight*10
#创造形象
“”“img=gimp.Image(cwidth*10,cheight*10,RGB)
img.disable_undo()
#保存当前前景色:
pdb.gimp_context_push()
#设置文本颜色和背景色
gimp.set_前景(彩色)
gimp.set_背景(0,0,0)
#创建所有层并相应定位
对于范围内的i(字符开始,字符结束):
字符串=“%c”%i
偏移量=i-字符开始
x_pos=偏移量*cwidth
y_pos=偏移量*cheight
text_layer=pdb.gimp_text_fontname(img,无,x_pos,y_pos,string,-1,False,大小,像素,字体)
gimp.progress_update(浮点(偏移)/浮点(num_字符))
pdb.gimp_图像展平(img)
img.enable_undo()
#创建一个新的图像窗口
gimp.显示器(img)
#显示新图像窗口
gimp.displays_flush()
#恢复旧的前景色:
pdb.gimp_context_pop()“”“
登记册(
“python\u fu\u位图\u字体”,
“位图字体”,
“创建新的位图字体”,
"*****",
"*****",
"2013",
“位图字体(Py)…”,
“”,#创建新映像,不处理现有映像
[
(PF_微调器,“cwidth”,“单元宽度”,24,(1,3000,1)),
(PF_SPINNER,“cheight”,“单元高度”,51,(1,3000,1)),
(字体、字体、字体面、控制台),
(PF_微调器,“大小”,“字体大小”,50,(1,3000,1)),
(PF_颜色,“颜色”,“文本颜色”(1.0,0.0,0.0))
],
[],
创建字体,菜单=“/File/create”)
main()
似乎在将开头的注释从第15行改为第19行之后,所有的事情都会变成地狱。老实说,我甚至不知道如何调试它。我尝试在Filters>PythonFu>console下使用控制台-但是这一直告诉我第1行是问题所在。。。我想我们都同意事实并非如此

我尝试在python脚本中运行这段代码,效果非常好


我该怎么办?

首先,尝试移除第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()"""

这是您注释代码的方式吗?

为什么要无缘无故地创建一个大字符串?这是你注释代码的方式吗?关于shebang,你试过把它去掉吗?也许GIMP真的想和他做点什么。你能回溯一下,或者提供更多关于到底发生了什么的信息吗?@Paco什么大字符串?我正在尝试创建一个包含所有95个字符的图像。-shebang有时有效,但我会试试。(结果:成功了)-我想它成功了,我想第1行是个问题。隐马尔可夫模型。。。让我在接下来的24小时里玩这个,只是为了确定。发布了一个答案来显示大字符串,还有我在评论中给出的答案,这是因为这是所有的代码,但在它被破坏后。。。(臭虫)。。。我试着发表评论,看看哪一行是问题所在。哈哈!最终,所有的大段评论都会消失。问题:为什么shebang是一个问题?它不应该是一个问题,因为它只是说这是应该使用的解释器。但通常我们不会放入“不可执行”的python文件(例如库)。也许它与GIMP有些冲突,我实际上从未为GIMP制作过任何插件。不应使用三重引号注释代码,这是一种糟糕的做法;)