Face whitening principle

Face whitening principle, is an image of the color space processing, so we need to design through the color space.

However, let’s first refer to the following photoshop processing steps for image whitening:

  1. First, create a new layer and set this layer to white
  2. Next, mix the white layer with the original image in the alpha channel, which will make the image white as a whole.

Through the operation of PS, we can roughly know that we need to create an image with the same size and dimension as the original image, and then assign all values to white, and then overlay the two images by image weighting and image.

However, there are obviously a lot of problems here. In Photoshop, we created an all-white layer, but we can crop or use the Brush tool to overlay only white on the character. In the case of the program, what we do is we make the whole image a little bit whiter, which is not ideal.

So, we need to consider a new idea to achieve face whitening effect.

According to the paper “A two-stage Contrast Enhancement Algorithm for Digital Images”, A mapping table is adopted to enhance the original image on the color level, and the brightness at both ends of the image is relatively weakened, while the middle is enhanced, which will produce A good whitening effect. And can make the image white more natural.

Here, we provide a whitening mapping table Color_list:

Color_list = [
	1.2.4.6.8.10.12.14.16.18.20.22.24.26.28.30.31.33.35.37.39.41.43.44.46.48.50.52.53.55.57.59.60.62.64.66.67.69.71.73.74.76.78.79.81.83.84.86.87.89.91.92.94.95.97.99.100.102.103.105.106.108.109.111.112.114.115.117.118.120.121.123.124.126.127.128.130.131.133.134.135.137.138.139.141.142.143.145.146.147.149.150.151.153.154.155.156.158.159.160.161.162.164.165.166.167.168.170.171.172.173.174.175.176.178.179.180.181.182.183.184.185.186.187.188.189.190.191.192.193.194.195.196.197.198.199.200.201.202.203.204.205.205.206.207.208.209.210.211.211.212.213.214.215.215.216.217.218.219.219.220.221.222.222.223.224.224.225.226.226.227.228.228.229.230.230.231.232.232.233.233.234.235.235.236.236.237.237.238.238.239.239.240.240.241.241.242.242.243.243.244.244.244.245.245.246.246.246.247.247.248.248.248.249.249.249.250.250.250.250.251.251.251.251.252.252.252.252.253.253.253.253.253.254.254.254.254.254.254.254.255.255.255.255.255.255.255.255.255.255.255.255.255.255.255.256]
Copy the code

Face whitening

Since the principle of face whitening, and whitening color mapping table are given to you. Below, we can achieve face whitening effect, the specific code is as follows:

def face_whitening(fileName) :
    img = cv2.imread(fileName)
    img = cv2.bilateralFilter(img, 19.75.75)
    height, width, n = img.shape
    img2 = img.copy()
    for i in range(height):
        for j in range(width):
            b = img2[i, j, 0]
            g = img2[i, j, 1]
            r = img2[i, j, 2]
            img2[i, j, 0] = Color_list[b]
            img2[i, j, 1] = Color_list[g]
            img2[i, j, 2] = Color_list[r]
    cv2.imwrite("59_1.jpg",img2)

    image = Image.open("59_1.jpg")
    # Sharpness adjustment
    enh_img = ImageEnhance.Sharpness(image)
    image_sharped = enh_img.enhance(1.2)
    Color balance adjustment
    con_img = ImageEnhance.Contrast(image_sharped)
    image_con = con_img.enhance(1.2)
    image_con.save("59_2.jpg")

    img1 = cv2.imread("58.jpg")
    img2 = cv2.imread("59_2.jpg")
    cv2.imshow("1", img1)
    cv2.imshow("2", img2)
    cv2.waitKey()
    cv2.destroyAllWindows()


if __name__ == "__main__":
    face_whitening("58.jpg")
Copy the code

After running, the effect is as follows: