问题
I am trying to write a plugin that will create a bitmap font. However, this is very frustrating to learn... while I am not familiar with python, it is not that hard to pick up and have not had problems with it outside of GIMP.
Copied some of the code from: https://github.com/sole/snippets/blob/master/gimp/generate_bitmap_font/sole_generate_bitmap_font.py and from http://gimpbook.com/scripting/
Does work:
#!/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()
Does not work:
#!/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()
It seems that the after changing the beginning comment from line 15 to line 19 that everything goes to hell. And to be honest, I am not even sure how to debug this. I tried using the console under Filters>Python-Fu>Console - however this kept telling me line 1 was the issue... which I think we can all agree is not the case.
I tried running pieces of this code in a python script and works perfectly fine.
What should I do?
回答1:
First of all, try to remove the shebang at line 1.
Then something that has nothing to with the actual problem, but why are you creating such a big string?
# 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()"""
Is this your way to comment out the code?
来源:https://stackoverflow.com/questions/18969820/plugins-usually-dont-work-how-do-i-debug