python - GIMP python 画线速度很慢

标签 python gimp gimpfu

pdb.gimp_paintbrush_default 似乎非常慢(使用标准画笔绘制 500 个点需要几秒钟。显然,线条更糟糕)。是这样吗?使用用户选择的画笔绘制直线时有没有办法加快速度?

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

最佳答案

我一直在研究非常类似的东西,也遇到了这个问题。我发现以下一项技术可以使我的函数速度提高大约 5 倍:

  1. 创建临时镜像
  2. 将您正在使用的图层复制到临时图像
  3. 在临时图层上进行绘图
  4. 将临时图层复制到原始图层之上

我相信这会加快速度,因为 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()

关于python - GIMP python 画线速度很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35681887/

相关文章:

python-3.x - 如何在python3脚本中导入gimp?

pdb - GIMP python-fu调用函数plug_in_beautify 'wrong number of parameters'

python - GIMP Python 无论我使用什么格式,我总是得到 "unable to parse color name"

python - 如何在 Gimp python 脚本中将信息输出到控制台?

Python:如何使用列表作为用户输入的选择源?

python - Cherrypy 可以访问,但没有显示任何内容

linux - Bash 在启动后写入后台作业的标准输入

opencv - 如何在 OpenCV 中重现 Photoshop 的多重混合?

python - 删除 Python 中用点分隔的字符串的最后一部分

Python:转换字符串以用于 ctypes.c_void_p()