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()
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)