美白,磨皮,搞笑图片处理
PIL库是一个支持图像存储、显示和处理的函数库,它能够处理几乎所有图像格式,可以完成对图像的缩放、裁剪、叠加以及图像添加条纹,文字等信息等的操作。
首先是利用pip下载一个:
它有许多子库,例如:Image、ImageChops、ImageDraw等等,他们各有神通,发挥着自己的作用,
首先我们当然学习的是经典的Image子库:
方法 | 描述 |
Image.open(filename) | 根据参数加载图像文件 |
Image.new(mode,size,color) | 根据给定参数创建一个新的图像 |
Image.frombytes(mode,size,data) | 根据像素点data创建图像 |
Image.verify() | 对图像文件完整性进行检查,返回异常 |
图像可视化:
from PIL import Image im = Image.open("C:\\Users\\ZK\\Desktop\\test1.jpg") # 读取图片 im.show() # 可视化
效果如下:
灰度化h
灰度化:
from PIL import Image img = Image.open("C:\\Users\\ZK\\Desktop\\test2.jpg") # 灰度化:将RGB/RGBA -> L img = img.convert("L") img.show()
原图:
效果如下:
美白磨皮:
from PIL import Image from PIL import ImageEnhance import cv2 import numpy as np # image = Image.open("C:\\Users\\ZK\\Desktop\\test1.jpg") #image.show() def BrightnessEnhancement(brightness): image = Image.open("C:\\Users\\ZK\\Desktop\\test1.jpg") enh_bri = ImageEnhance.Brightness(image) # brightness =1.5 image_brightened = enh_bri.enhance(brightness) image_brightened.show() def ContrastEnhancement(contrast): image = Image.open("C:\\Users\\ZK\\Desktop\\test1.jpg") enh_con = ImageEnhance.Contrast(image) # contrast =1.5 image_contrasted = enh_con.enhance(contrast) image_contrasted.show() def ColorEnhancement(color): image = Image.open("C:\\Users\\ZK\\Desktop\\test1.jpg") enh_col = ImageEnhance.Color(image) # color =0.8 image_colored = enh_col.enhance(color) image_colored.show() def SharpnessEnhancement(sharpness): image = Image.open("C:\\Users\\ZK\\Desktop\\test1.jpg") enh_sha = ImageEnhance.Sharpness(image) # sharpness = 2 image_sharped = enh_sha.enhance(sharpness) image_sharped.show() def Filter(image): image =cv2.imread("C:\\Users\\ZK\\Desktop\\test1.jpg") Remove=cv2.bilateralFilter(image,0,0,10) cv2.imshow('filter',Remove) cv2.waitKey(0) cv2.destroyAllWindows() # res = np.uint8(np.clip((1.2 * image + 10), 0, 255)) # tmp = np.hstack((dst, res)) # cv2.imshow('bai',res) def WhiteBeauty(image,whi): image =cv2.imread("C:\\Users\\ZK\\Desktop\\test1.jpg") white = np.uint8(np.clip((whi * image + 50), 0, 255)) cv2.imshow('bai',white) cv2.waitKey(0) cv2.destroyAllWindows() if __name__ =="__main__": filepath = "C:\\Users\\ZK\\Desktop\\test1.jpg" #原始图像 brightness = 1.5 contrast = 0.2 color=1.9 sharpness=0.1 BrightnessEnhancement(brightness) ContrastEnhancement(contrast) ColorEnhancement(color) SharpnessEnhancement(sharpness) whi = 1.2 image =cv2.imread("C:\\Users\\ZK\\Desktop\\test1.jpg") Filter(image) WhiteBeauty(image,whi)
原图:
美白:
灰度处理: