我一直工作在非常相似的東西,就遇到了這個問題也。這裏有一個技巧,我發現了我的函數快約5倍:
- 創建一個臨時圖像
- 複製您正在使用的臨時圖像
- 工作層執行臨時層
上繪圖
- 上覆制原圖層
我相信這個速度的東西了,因爲GIMP沒有繪製編輯屏幕,但我不是100%肯定的頂部的臨時層。這是我的功能:
def splotches(img, layer, size, variability, quantity):
gimp.context_push()
img.undo_group_start()
width = layer.width
height = layer.height
temp_img = pdb.gimp_image_new(width, height, img.base_type)
temp_img.disable_undo()
temp_layer = pdb.gimp_layer_new_from_drawable(layer, temp_img)
temp_img.insert_layer(temp_layer)
brush = pdb.gimp_brush_new("Splotch")
pdb.gimp_brush_set_hardness(brush, 1.0)
pdb.gimp_brush_set_shape(brush, BRUSH_GENERATED_CIRCLE)
pdb.gimp_brush_set_spacing(brush, 1000)
pdb.gimp_context_set_brush(brush)
for i in range(quantity):
random_size = size + random.randrange(variability)
x = random.randrange(width)
y = random.randrange(height)
pdb.gimp_context_set_brush_size(random_size)
pdb.gimp_paintbrush(temp_layer, 0.0, 2, [x, y, x, y], PAINT_CONSTANT, 0.0)
gimp.progress_update(float(i)/float(quantity))
temp_layer.flush()
temp_layer.merge_shadow(True)
# Delete the original layer and copy the new layer in its place
new_layer = pdb.gimp_layer_new_from_drawable(temp_layer, img)
name = layer.name
img.remove_layer(layer)
pdb.gimp_item_set_name(new_layer, name)
img.insert_layer(new_layer)
gimp.delete(temp_img)
img.undo_group_end()
gimp.context_pop()
謝謝,我用你的方法寫了一個小基準,但我得到了任何形式的非確定性結果;有時較慢,有時較快。如果你想看看,這裏是[鏈接](http://www.mikropunto.org/wp-content/uploads/paintbrush_default_benchmark.tar.gz) – mikro
我認爲如果你打一個電話,情況可能會有所不同到gimp_paintbrush_default與一堆點,並與一個單一的點幾百個電話。另一件可能會加速我的代碼中的功能是「temp_img.disable_undo()」調用。如果GIMP不需要記錄任何撤消信息,它可能會加快速度。雖然這可能不會影響到gimp_paintbrush_default的一個大調用。 – Stephen