Search code examples
pythonscriptinggimp

How do I do the equivalent of Gimp's Colors, Auto, White Balance in Python-Fu?


the only function I can find is : gimp-color-balance, which takes the applicable parameters : preserve-lum(osity), cyan-red, magenta-green, and yellow-blue.

I'm not sure what values to pass for these parameters to duplicate the menu option in the title.


Solution

  • To complete the answer of @banderlog013, I think the Gimp Doc specifies that the end pixels of each channel are first discarded, then the remaining ranges are stretched. I believe the right code is :

    img = cv2.imread('test.jpg')
    balanced_img = np.zeros_like(img) #Initialize final image
    
    for i in range(3): #i stands for the channel index 
        hist, bins = np.histogram(img[..., i].ravel(), 256, (0, 256))
        bmin = np.min(np.where(hist>(hist.sum()*0.0005)))
        bmax = np.max(np.where(hist>(hist.sum()*0.0005)))
        balanced_img[...,i] = np.clip(img[...,i], bmin, bmax)
        balanced_img[...,i] = (balanced_img[...,i]-bmin) / (bmax - bmin) * 255
    

    I obtain good results with it, try it out !