2

I have a Scriptfu script written in Python for Gimp which applies several steps on an existing image and converts it to an indexed image in the process. The lightest color in the resulting image is always nearly white; I want to set it to exactly white. Conveniently, that lightest color is always the topmost color in the colormap of the indexed image, so I just want to set the topmost color in the colormap to white.

I have found nothing in the API description on how to manipulate the colormap (i. e. colors therein), so currently that step I always do manually (Windows → Dockable Dialogs → Colormap → Click on the topmost color → enter "ffffff" in the text widget → close dialog). But of course the whole idea of the Scriptfu stuff is to automate all steps, not just a few.

Can anybody tell me how to access the colormap from a Python Scriptfu script?

Here is my current code (which does not even attempt to perform that last step, due to lack of ideas on how to do it):

#!/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()
Charles
  • 50,943
  • 13
  • 104
  • 142
Alfe
  • 56,346
  • 20
  • 107
  • 159
  • 1
    Just for the record, in GIMPland, we usually reserve the term "script-fu" to code written in Scheme (the language). A Python script could be called "Python-fu" instead (although I prefer calling it simply a Python script for GIMP) – jsbueno Mar 26 '14 at 02:03
  • This is my first script for Gimp ever and being brand new to the topic, I'm bound to make such mistakes, so thanks for the clarification! – Alfe Mar 26 '14 at 09:00

1 Answers1

1

just use pdb.gimp_image_get_colormap and pdb.gimp_image_set_colormap.

If the entry you want to change is indeed always the first, it would suffice to write:

colormap = pdb.gimp_image_get_colormap(timg)[1]
colormap = (255,255,255) + colormap[3:]
pdb.gimp_image_set_colormap(timg, len(colormap), colormap)
jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • Thank you, your solution works perfectly! In fact my lightest color always was the one with the highest index, not with index 0, but given your answer it was easy to be thorough and scan for the lightest color and patch it to be white: `colors = [ colormap[i:i+3] for i in range(0, len(colormap), 3) ]` `enumeratedColors = list(enumerate(colors))` `indexOfLightest, lightest = max(enumeratedColors, key=lambda (n, (r, g, b)): r + g + b)` `colors[indexOfLightest] = (255, 255, 255)` `colormap = sum(map(list, colors), [])` – Alfe Mar 25 '14 at 09:30