2016-02-28 160 views
2

pdb.gimp_paintbrush_default似乎很慢(使用標準畫筆幾秒鐘,對於500個點,線條明顯變差)。這是這樣嗎?使用用戶選擇的筆刷繪製直線時,有沒有辦法加快速度?GIMP蟒蛇畫線很慢

pythonfu控制檯代碼:

from random import randint 
img=gimp.image_list()[0] 
drw = pdb.gimp_image_active_drawable(img) 

width = pdb.gimp_image_width(img) 
height = pdb.gimp_image_height(img) 

point_number = 500 
while (point_number > 0): 
    x = randint(0, width) 
    y = randint(0, height) 
    pdb.gimp_paintbrush_default(drw,2,[x,y]) 
    point_number -= 1 

回答

2

我一直工作在非常相似的東西,就遇到了這個問題也。這裏有一個技巧,我發現了我的函數快約5倍:

  1. 創建一個臨時圖像
  2. 複製您正在使用的臨時圖像
  3. 工作層執行臨時層
  4. 上繪圖
  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() 
+0

謝謝,我用你的方法寫了一個小基準,但我得到了任何形式的非確定性結果;有時較慢,有時較快。如果你想看看,這裏是[鏈接](http://www.mikropunto.org/wp-content/uploads/paintbrush_default_benchmark.tar.gz) – mikro

+2

我認爲如果你打一個電話,情況可能會有所不同到gimp_paintbrush_default與一堆點,並與一個單一的點幾百個電話。另一件可能會加速我的代碼中的功能是「temp_img.disable_undo()」調用。如果GIMP不需要記錄任何撤消信息,它可能會加快速度。雖然這可能不會影響到gimp_paintbrush_default的一個大調用。 – Stephen