Gimp, Script-Fu: 如何直接在调色板中设置值

2 投票
1 回答
2114 浏览
提问于 2025-04-17 23:44

我有一个用Python写的Scriptfu脚本,专门用于Gimp这个图像处理软件。这个脚本会对已有的图片进行多个处理步骤,并在这个过程中把图片转换成索引图像。结果图像中最浅的颜色总是接近白色,但我想把它设置成完全的白色。幸运的是,这个最浅的颜色总是在索引图像的颜色表中排在最上面,所以我只需要把颜色表中的最上面颜色设置为白色。

我在API的说明中没有找到关于如何操作颜色表(也就是里面的颜色)的相关信息,所以目前这一步我都是手动完成的(路径是:窗口 → 可停靠对话框 → 颜色表 → 点击最上面的颜色 → 在文本框中输入“ffffff” → 关闭对话框)。但Scriptfu的整个目的就是为了自动化所有步骤,而不仅仅是部分步骤。

有没有人能告诉我如何在Python Scriptfu脚本中访问颜色表?

这是我目前的代码(因为不知道怎么做最后一步,所以连这一步都没有尝试):

#!/usr/bin/env python

"""
paperwhite -- a gimp plugin (place me at ~/.gimp-2.6/plug-ins/ and give
              me execution permissions) for making fotographs of papers
              (documents) white in the background
"""

import math
from gimpfu import *

def python_paperwhite(timg, tdrawable, radius=12):
    layer = tdrawable.copy()
    timg.add_layer(layer)
    layer.mode = DIVIDE_MODE
    pdb.plug_in_despeckle(timg, layer, radius, 2, 7, 248)
    timg.flatten()
    pdb.gimp_levels(timg.layers[0], 0, 10, 230, 1.0, 0, 255)
    pdb.gimp_image_convert_indexed(timg,
        NO_DITHER, MAKE_PALETTE, 16, False, True, '')
    (bytesCount, colorMap) = pdb.gimp_image_get_colormap(timg)
    pdb.gimp_message("Consider saving as PNG now!")

register(
        "python_fu_paperwhite",
        "Make the paper of the photographed paper document white.",
        "Make the paper of the photographed paper document white.",
        "Alfe Berlin",
        "Alfe Berlin",
        "2012-2012",
        "<Image>/Filters/Artistic/Paperw_hite...",
        "RGB*, GRAY*",
        [
                (PF_INT, "radius", "Radius", 12),
        ],
        [],
        python_paperwhite)

main()

1 个回答

1

只需使用 pdb.gimp_image_get_colormappdb.gimp_image_set_colormap 这两个命令。

如果你想要更改的条目确实总是第一个,那么只需要写:

colormap = pdb.gimp_image_get_colormap(timg)[1]
colormap = (255,255,255) + colormap[3:]
pdb.gimp_image_set_colormap(timg, len(colormap), colormap)

撰写回答