#!/usr/bin/env python # # ================================================================================ # ColourNegativeInverter.py - (c) Berthold Hinz 2009 # ================================================================================ # # COLOUR NEGATIVE INVERTER FOR THE GIMP V.3 # # ================================================================================ # # INSTRUCTIONS: # # Script needs the colour of unexposed film as reference. Either the scanned image # includes a small border or edge of unexpoded film or you have to set the Gimp # foereground colour to the colour of unexposed film. # # Change step-value to influence processing speed and performance. Step value of # 1 means that each single pixel is taken into account. The higher the step-value # the more pixels will be skiped. The script will work faster, but the probability # to ignore important parts of the image will gain too. # # ================================================================================ # # This program is free software; you can redistribute it and/or modify it. # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # # ================================================================================ from gimpfu import * import pygtk pygtk.require("2.0") import gtk def CNI(img, drw, incl, contr_enh, step, merge_layers): pdb.gimp_image_undo_group_start(img) if incl==True: LightMax=0 pdb.gimp_message("Processing ..." +"\n") for i in range (1, img.width-2,step): for j in range (1, img.height-2,step): val = getpixel(drw, i, j) lightness = getV(val[0],val[1],val[2]) if lightness > LightMax: LightMax=lightness LMax_R=val[0] LMax_G=val[1] LMax_B=val[2] gimp.set_foreground(LMax_R,LMax_G,LMax_B) else: fgc = gimp.get_foreground() LMax_R=fgc[0] LMax_G=fgc[1] LMax_B=fgc[2] layer_copy = drw.copy(True) layer_copy.mode = NORMAL_MODE layer_copy.name = "bias removed" img.add_layer(layer_copy,0) drw=img.layers[0] pdb.gimp_curves_spline(drw, 1, 4,[0, 0, LMax_R, 255]) pdb.gimp_curves_spline(drw, 2, 4,[0, 0, LMax_G, 255]) pdb.gimp_curves_spline(drw, 3, 4,[0, 0, LMax_B, 255]) if contr_enh==True: pdb.gimp_levels_stretch(drw) pdb.gimp_message("Done!") layer_copy = drw.copy(True) layer_copy.mode = NORMAL_MODE layer_copy.name = "inverted" img.add_layer(layer_copy,0) drw=img.layers[0] pdb.gimp_invert(drw) if merge_layers==True: pdb.gimp_image_merge_down(img,drw,0) pdb.gimp_image_merge_down(img,img.layers[0],0) pdb.gimp_image_undo_group_end(img) def getpixel(drw, x, y): """ gets colour values """ tile = drw.get_tile2(False, x, y) x_offset = x % 64 y_offset = y % 64 pixel = tile[x_offset, y_offset] values = [] for i in range(len(pixel)): values.append(ord(pixel[i])) return values def getV(R, G, B): mi=min(R,G,B)*1.0 ma=max(R,G,B)*1.0 v = round(ma/255*100) return v register( "colour_negative_inverter", "Removes orange bias from scanned colour negatives and inverts result"+"\n\n"+"(if you don't use auto-mode, set Gimp foreground colour to colour"+"\n" +"of unexposed film before you run the script)", "", "Berthold Hinz>", "GPL", "2009", "/Filters/Photo/Colour Negative Inverter", "RGB*", [(PF_TOGGLE,"incl","Auto-mode (best if image includes parts of unexposed film):", True), (PF_TOGGLE,"contr_enh","Contrast enhancement:", True), (PF_SLIDER,"step","Speed (only relevant for auto-mode):",10,(1,100,10)), (PF_TOGGLE,"merge_layers","Merge layers:", False), ], [], CNI) main()