Looks like you are re-inventing the automatic contrast stretch, either
Also, if you batch-process, using ImageMagick instead of Gimp is likely be a better idea.
Code:
pdb.gimp_drawable_levels_stretch(drawable)
pdb.plug_in_autostretch_hsv(image, drawable)
Also, if you batch-process, using ImageMagick instead of Gimp is likely be a better idea.