#!/usr/bin/env python # -*- coding: utf-8 -*- from gimpfu import * import numarray as Numarray def custom_layer_op(layer_1, layer_2, threshold) : width = layer_1.width height = layer_1.height pixel_region_1 = layer_1.get_pixel_rgn (0, 0, width, height) array_1 = Numarray.array (pixel_region_1[:,:], Numarray.Int8) px_1 = Numarray.reshape (array_1, (width, height, 3 + layer_1.has_alpha)) if not layer_2.has_alpha: layer_2.add_alpha() pixel_region_2 = layer_2.get_pixel_rgn (0, 0, layer_2.width, layer_2.height) array_2 = Numarray.array (pixel_region_2[:,:], Numarray.Int8) px_2 = Numarray.reshape (array_2, (width, height, 4)) if layer_1.has_alpha : p1 = Numarray.take(px_1, (0,1,2), 2 ) else : p1 = px_1 p2 = Numarray.take(px_2, (0,1,2), 2 ) c = ((p1 ^ p2) & -128) r = (c == 0) * (p1 - Numarray.clip(p1 - p2, -threshold, threshold)) + (c != 0) * p1 dest_pixels = Numarray.concatenate((r, Numarray.take(px_2,(3,),2)) , 2) pixel_region_2[:,:] = dest_pixels.tostring() layer_2.update (0, 0, width, height) def noise_remover(img, drawable, noiserad, thresold, radius, th) : pdb.gimp_image_undo_group_start(img) #Decompose image to LAB lab_image = pdb.plug_in_decompose(img, drawable, "LAB", True)[0] # Blur B and A channels pdb.plug_in_gauss_rle(lab_image, lab_image.layers[0], noiserad, 1, 1) pdb.plug_in_gauss_rle(lab_image, lab_image.layers[1], noiserad, 1, 1) #Compose LAB to RGB result_img = pdb.plug_in_drawable_compose(lab_image, \ lab_image.layers[2], \ lab_image.layers[1], \ lab_image.layers[0], None, "LAB") #Delete LAB image pdb.gimp_image_delete(lab_image) # Selective Gaussian Blur on result image pdb.plug_in_sel_gauss(result_img, result_img.layers[0], radius, thresold) top_layer = img.layers[0] l = pdb.gimp_layer_new_from_drawable(result_img.layers[0], img) img.add_layer(l, -1) custom_layer_op(top_layer, l, int(th)) img.remove_layer(top_layer) # Delete result image pdb.gimp_image_delete(result_img) pdb.gimp_image_undo_group_end (img) register ("noise_remover" , "Remove color noise by bluring B and A channels in LAB image" , "Timothey Sleptsov " , "Tim sleptsov" , "2004-09-09" , "" , "/Filters/Noise/Remove" , "RGB*", [(PF_VALUE, "noiserad", "Noise Blur Radius", 20.0) , (PF_VALUE, "thresold", "Thresold", 15) , (PF_VALUE, "radius", "Radius", 5.0), (PF_VALUE, "th", "Color Guard", 20) ], [], noise_remover) main()