Title

好玩Python——PIL项目实训(四)——美白磨皮

  1 # 图像基础调整: 图像的亮度、对比度、色度,还可以用于增强图像的锐度,美白
  2 # """
  3 
  4 from PIL import Image
  5 from PIL import ImageEnhance
  6 import cv2
  7 import numpy as np
  8 
  9 
 10 # image = Image.open('14.jpg')
 11 #image.show()
 12 def BrightnessEnhancement(brightness):
 13     # '''
 14     # #亮度增强 :brightness在(0-1)之间,新图像较原图暗,在(1-~)新图像较原图亮 ,
 15     # ##brightness=1,保持原图像不变;可自定义参数范围
 16     # '''
 17     image = Image.open(filepath)
 18     enh_bri = ImageEnhance.Brightness(image)
 19 #    brightness =1.5
 20     image_brightened = enh_bri.enhance(brightness)
 21     image_brightened.show()
 22 
 23 def ContrastEnhancement(contrast):
 24     # '''
 25     # #对比度增强: 可自定义参数contrast范围,contrast=1,保持原图像不变
 26     # '''
 27     image = Image.open(filepath)
 28     enh_con = ImageEnhance.Contrast(image)
 29 #    contrast =1.5
 30     image_contrasted = enh_con.enhance(contrast)
 31     image_contrasted.show()
 32 
 33 def ColorEnhancement(color): 
 34     # '''
 35     # #色度增强 : 饱和度  color=1,保持原图像不变
 36     # '''
 37     image = Image.open(filepath)
 38     enh_col = ImageEnhance.Color(image)
 39 #    color =0.8
 40     image_colored = enh_col.enhance(color)
 41     image_colored.show()
 42 
 43 def SharpnessEnhancement(sharpness):
 44     # '''
 45     # #锐度增强: 清晰度  sharpness=1,保持原图像不变
 46     # '''
 47     image = Image.open(filepath)
 48     enh_sha = ImageEnhance.Sharpness(image)
 49 #    sharpness = 2
 50     image_sharped = enh_sha.enhance(sharpness)
 51     image_sharped.show()
 52 
 53 def Filter(image):
 54     # """
 55     # 色彩窗的半径
 56     # 图像将呈现类似于磨皮的效果
 57     # """
 58     #image:输入图像,可以是Mat类型,
 59     #       图像必须是8位或浮点型单通道、三通道的图像
 60     #0:表示在过滤过程中每个像素邻域的直径范围,一般为0
 61     #后面两个数字:空间高斯函数标准差,灰度值相似性标准差
 62     image =cv2.imread(filepath)
 63     Remove=cv2.bilateralFilter(image,0,0,10)
 64     cv2.imshow('filter',Remove)
 65     cv2.waitKey(0)
 66     cv2.destroyAllWindows()
 67 #    res = np.uint8(np.clip((1.2 * image + 10), 0, 255))
 68 #    tmp = np.hstack((dst, res)) 
 69 #    cv2.imshow('bai',res)
 70 
 71 
 72 def WhiteBeauty(image,whi):
 73     # '''
 74     # 美白
 75     # '''
 76     import cv2
 77 
 78     image =cv2.imread(filepath)
 79     white = np.uint8(np.clip((whi * image + 10), 0, 255))
 80     cv2.imshow('bai',white)
 81     cv2.waitKey(0)
 82     cv2.destroyAllWindows()
 83 
 84 
 85 
 86 if __name__ =="__main__":
 87     filepath = 'D:\\python\\img\\WDC.jpg'
 88     #原始图像
 89     brightness = 1.5
 90     contrast = 0.2
 91     color=1.9
 92     sharpness=0.1
 93     BrightnessEnhancement(brightness)
 94     ContrastEnhancement(contrast)
 95     ColorEnhancement(color)
 96     SharpnessEnhancement(sharpness)
 97     whi = 1.2
 98     image =cv2.imread('D:\\python\\img\\WDC.jpg')
 99     Filter(image)
100     WhiteBeauty(image,whi)

 



 

原图为:

posted @ 2020-04-14 03:01  赖金明的大爹  阅读(838)  评论(0编辑  收藏  举报