好的,我正在尝试为 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 行和第 33 - 40 行时,我收到以下错误。
我希望我的最终代码看起来像这样:
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.py和http://gimpbook.com/scripting/
跑 'python -tt plugin.py' 并得到以下信息: