GIMP/Scripts/PaperContrast
Jump to navigation
Jump to search
- Script name: PaperContrast
- Author: Tintazul
- Summary: Previous to calling PaperContrast, the user should select a small rectangle of the page background as an indication of what should be whitened out. Script operation: 1. remove transparency (if any); 2. convert to grayscale (if not already); 3. select darkest pixel from selection (if any, sampling max 8 thousand pixels); 4. auto-stretch contrast; 5. set white point to darkest pixel of selection, black point to half of white point; 6. save
#!/usr/bin/env python from gimpfu import * def paper_contrast (image, layer): max_pixels = 8000 #limits number of pixels to be sampled max_blackpoint = 96 #all pixels darker than this will be pure black #remove transparency if it has it if pdb.gimp_drawable_has_alpha (layer): layer = pdb.gimp_image_flatten (image) #get format #convert to grayscale if not already if not pdb.gimp_drawable_is_gray (layer): pdb.gimp_image_convert_grayscale (image) #collect min pixel from selection whitepoint = 255 (non_empty, x1, y1, x2, y2) = pdb.gimp_selection_bounds (image) if non_empty: #limit number of pixels to be sampled, in case user made too big a selection max_cols = int (max_pixels / (y2-y1+1)) x2 = min (x2, x1+max_cols-1) #collect all pixel values for x in range(x1,x2): for y in range(y1,y2): (channels, pixels) = pdb.gimp_drawable_get_pixel (layer,x,y) whitepoint = min (whitepoint, pixels[0]) #remove selection so that levels options apply to all image pdb.gimp_selection_none(image) #auto-strech, then stretch contrast pdb.gimp_levels_stretch (layer) blackpoint = min (max_blackpoint, int (whitepoint/2)) print 'blackpoint:', blackpoint, 'whitepoint:', whitepoint pdb.gimp_levels (layer, 0, blackpoint, whitepoint, 1.0, 0, 255) #update screen, save and remove dirty bit pdb.gimp_displays_flush () name = pdb.gimp_image_get_filename(image) pdb.gimp_file_save(image, layer, name, name) pdb.gimp_image_clean_all(image) register( "python_fu_paper_contrast", "Increases contrast of a picture with a paper background", "Increases contrast of a picture with a paper background. 1) flatten image; 2) convert to grayscale; 3) if there's a selection, get darkest pixel in selection; 4) auto-stretch contrast; 5) stretch levels to make darkest pixel in selection white; 6) save file.", "Julio Reis", #very important note: won't allow accented u "Julio Reis", "2008-08-06", "<Image>/_Gutenberg/_PaperContrast", "RGB*, GRAY*, INDEXED*", [], [], paper_contrast)