修改後的程式碼,與測試的結果圖
blur_radius = 0
size = (blur_radius + 20) / 2
density = 60
cell_size = 7
string = "Newsprint"
font_size = 100
font = "Arial"
text_ext = pdb.gimp_text_get_extents_fontname(string, font_size, PIXELS, font)
width = text_ext[0] + 20 + blur_radius
height= text_ext[1] + 20 + blur_radius
text_color = (0, 0, 0)
bg_color = (255, 255, 255)
img = pdb.gimp_image_new(width, height, RGB)
pdb.gimp_display_new(img)
bg_layer = gimp.Layer(img, "Background", width, height, RGB_IMAGE, 100, NORMAL_MODE)
text_layer = gimp.Layer(img, "Text layer", width, height, RGBA_IMAGE, 100, NORMAL_MODE)
#pdb.gimp_image_undo_disable(img)
img.add_layer(bg_layer, 1)
img.add_layer(text_layer, -1)
text_mask = 0
grey = (density * 255) / 100
gimp.set_background(bg_color)
pdb.gimp_edit_clear(bg_layer)
pdb.gimp_edit_clear(text_layer)
gimp.set_foreground(text_color)
tmp_layer = pdb.gimp_text_fontname(img, text_layer, size, size ,string, 0, TRUE, font_size, PIXELS, font)
pdb.gimp_floating_sel_anchor(tmp_layer)
text_mask = pdb.gimp_layer_create_mask(text_layer, ADD_ALPHA_MASK)
pdb.gimp_layer_add_mask(text_layer, text_mask)
pdb.gimp_selection_layer_alpha(text_layer)
#pdb.gimp_context_set_background(grey, grey, grey)
gimp.set_background(grey, grey, grey)
pdb.gimp_edit_fill(text_mask, BACKGROUND_FILL)
pdb.gimp_selection_none(img)
blur_radius = 5
if blur_radius > 0:
pdb.plug_in_gauss_iir(img, text_mask, blur_radius, 1, 1) # 不需要 RUN_NONINTERACTIVE 這個參數
pdb.plug_in_newsprint(img, text_mask, cell_size, 0, 0, 45.0, 3, 45.0, 0, 45.0, 0, 45.0, 0, 3) # 不需要 RUN_NONINTERACTIVE 這個參數
pdb.gimp_edit_fill(text_layer, FOREGROUND_FILL)
pdb.gimp_layer_remove_mask(text_layer, MASK_APPLY)
#pdb.gimp_image_undo_enable(img)
#pdb.gimp_display_new(img)
#pdb.gimp_context_pop()