Welcome, guest | Sign In | My Account | Store | Cart

This Gimp-plugin (written in Python) changes a photograph of a paper document so that the paper background appears white again without overly lighting the text.

Python, 50 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/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)
    colormap = pdb.gimp_image_get_colormap(timg)[1]
    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)
    indexOfDarkest, darkest = min(enumeratedColors,
        key=lambda (n, (r, g, b)): r + g + b)
    colors[indexOfLightest] = (255, 255, 255)
    colors[indexOfDarkest] = (0, 0, 0)
    colormap = sum(map(list, colors), [])
    pdb.gimp_image_set_colormap(timg, len(colormap), colormap)
    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()