Home » Python » Increase brightness of specific pixels in an image using python-Exceptionshub

Increase brightness of specific pixels in an image using python-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment


I would like to increase the brightness/vividness of the purple color in the following image:

enter image description here

Here is the color palette

enter image description here

Here is what I tried: but this increases the brightness of the whole image:

def increase_brightness(img, value=20):
    hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
    h, s, v = cv2.split(hsv)

    lim = 255 - value
    v[v > lim] = 255
    v[v <= lim] += value

    final_hsv = cv2.merge((h, s, v))
    img = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)
    plt.imsave('img_new.png', img)

    return img

how to create a mask to modify brightness only the pixels in the input that corresponds to purple?

How to&Answers:

Note you have converted the image from RGB (to HSV) and need to convert it from BGR (to HSV).

If you only want to increase the brightness of the purple, then use cv2.inRange() for the purple color to create a mask. Then modify the input image everywhere with your current method. Then use the mask to combine the input and modified images so as to only show the enhancement for the purple colors corresponding to the white in the mask.

So this is one to do that in Python/OpenCV.


enter image description here

import cv2
import numpy as np

# read image
img = cv2.imread('purple.png')

# set value
value = 20

# convert image to hsv colorspace
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)

# create mask on purple color and also its inverted mask
low_range = (80,160,50)
high_range = (150,230,120)
mask = cv2.inRange(hsv,low_range,high_range)
inv_mask = cv2.bitwise_not(mask)
mask = cv2.merge([mask,mask,mask])
inv_mask = cv2.merge([inv_mask,inv_mask,inv_mask])

# enhance the value channel of the hsv image
lim = 255 - value
v[v > lim] = 255
v[v <= lim] += value

# convert it back to BGR colors
final_hsv = cv2.merge((h, s, v))
bgr = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)

# use bit_wise_and and its inverse to combine the original and enhanced versions
bgr = cv2.bitwise_and(bgr,mask)
img = cv2.bitwise_and(img,inv_mask)
result = cv2.add(bgr,img)

# display IN and OUT images
cv2.imshow('IMAGE', img)
cv2.imshow('HSV', hsv)
cv2.imshow('MASK', mask)
cv2.imshow('RESULT', result)

# save output image
cv2.imwrite('purple_enhanced.png', result)


enter image description here

If you alternate viewing of the input and output, you will see that the output is brighter everywhere.


You can add a contrast to your image. It is not possible to reuse the code, but create one that considers the contrast:

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

image = cv.imread('image.png')

def increase_brightness(image, alpha, beta):
    # Simple contrast control(alpha)
    # Simple brightness control(betha)

    new_image = np.zeros(image.shape, image.dtype)

    for y in range(image.shape[0]):
        for x in range(image.shape[1]):
            for c in range(image.shape[2]):
                new_image[y,x,c] = np.clip(alpha*image[y,x,c] + beta, 0, 255)

    plt.imsave('img_new.png', new_image)
    return new_image

I tested the following case:

increase_brightness(image, 1.0, 4)

Old image:

enter code here

New image:

enter image description here

My solution is based on this link: https://docs.opencv.org/3.4/d3/dc1/tutorial_basic_linear_transform.html