【问题标题】:Gimp, Script-Fu: How can I set a value in the colormap directlyGimp,Script-Fu:如何直接在颜色图中设置值
【发布时间】:2014-05-02 13:48:15
【问题描述】:

我有一个用 Python 为 Gimp 编写的 Scriptfu 脚本,它在现有图像上应用几个步骤,并在此过程中将其转换为索引图像。结果图像中最亮的颜色总是接近白色;我想将它设置为完全白色。方便的是,最亮的颜色始终是索引图像的颜色图中最上面的颜色,所以我只想将颜色图中的最上面的颜色设置为白色。

我在 API 描述中没有找到关于如何操作颜色图(即其中的颜色)的任何内容,所以目前我总是手动执行该步骤(Windows → Dockable Dialogs → Colormap → 单击最上面的颜色 → 在文本小部件 → 关闭对话框)。当然,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()

【问题讨论】:

  • 为了记录,在 GIMPland 中,我们通常将术语“script-fu”保留给用 Scheme(该语言)编写的代码。 Python 脚本可以称为“Python-fu”(尽管我更喜欢将其简单地称为 GIMP 的 Python 脚本)
  • 这是我为 Gimp 编写的第一个脚本,而且对这个主题是全新的,我一定会犯这样的错误,所以感谢您的澄清!

标签: python gimp script-fu gimpfu


【解决方案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)

【讨论】:

  • 谢谢,您的解决方案完美运行!事实上,我最浅的颜色始终是索引最高的颜色,而不是索引 0,但鉴于您的回答,很容易彻底扫描并扫描最浅的颜色并将其修补为白色:colors = [ colormap[i:i+3] for i in range(0, len(colormap), 3) ]enumeratedColors = list(enumerate(colors))@987654326 @colors[indexOfLightest] = (255, 255, 255)colormap = sum(map(list, colors), [])
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-30
  • 1970-01-01
相关资源
最近更新 更多