Python+QT美颜工具源码

简介: Python+QT美颜工具源码
程序示例精选
Python+QT美颜工具源码
如需安装运行环境或远程调试,可点击
博主头像进入个人主页查看博主联系方式,由专业技术人员远程协助!

前言

这篇博客针对《Python+QT美颜工具源码》编写代码,代码整洁,规则,易读。 学习与应用推荐首选。

运行结果

文章目录

一、所需工具软件
二、使用步骤
1. 主要代码
2. 运行结果
三、在线协助

一、所需工具软件

1. Pycharm,Python
2. Qt

二、使用步骤

代码如下(示例):

# 饱和度
    def change_saturation(self):
        if self.raw_image is None:
            return 0

        value = self.ui.horizontalSlider.value()
        img_hsv = cv2.cvtColor(self.current_img, cv2.COLOR_BGR2HLS)
        if value > 2:
            img_hsv[:, :, 2] = np.log(img_hsv[:, :, 2] /255* (value - 1)+1) / np.log(value + 1) * 255
        if value < 0:
            img_hsv[:, :, 2] = np.uint8(img_hsv[:, :, 2] / np.log(- value + np.e))
        self.current_img = cv2.cvtColor(img_hsv, cv2.COLOR_HLS2BGR)

# 明度调节
    def change_darker(self):
        if self.raw_image is None:
            return 0
        value = self.ui.horizontalSlider_4.value()
        img_hsv = cv2.cvtColor(self.current_img, cv2.COLOR_BGR2HLS)
        if value > 3:
            img_hsv[:, :, 1] = np.log(img_hsv[:, :, 1] /255* (value - 1)+1) / np.log(value + 1) * 255
        if value < 0:
            img_hsv[:, :, 1] = np.uint8(img_hsv[:, :, 1] / np.log(- value + np.e))
        self.last_image = self.current_img
        self.current_img = cv2.cvtColor(img_hsv, cv2.COLOR_HLS2BGR)

# 人脸识别
    def detect_face(self):
        img = self.raw_image
        face_cascade = cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')

        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)
        return faces

# 皮肤识别
    def detect_skin(self):
        img = self.raw_image
        rows, cols, channals = img.shape
        for r in range(rows):
            for c in range(cols):
                B = img.item(r, c, 0)
                G = img.item(r, c, 1)
                R = img.item(r, c, 2)
                if (abs(R - G) > 15) and (R > G) and (R > B):
                    if (R > 95) and (G > 40) and (B > 20) and (max(R, G, B) - min(R, G, B) > 15):
                        self.imgskin[r, c] = (1, 1, 1)
                    elif (R > 220) and (G > 210) and (B > 170):
                        self.imgskin[r, c] = (1, 1, 1)

# 皮肤磨皮(value1精细度,value2程度)
    def dermabrasion(self, value1=3, value2=2):
        value1 = self.ui.horizontalSlider_14.value()
        value2 = 11 - self.ui.horizontalSlider_11.value()
        if value1 == 0 and value2 == 0:
            return 0
        if value2 == 0:
            value2 = 2
        if value1 == 0:
            value1 = 3
        img = self.current_img
        dx = value1 * 5
        fc = value1 * 12.5
        p = 50
        temp1 = cv2.bilateralFilter(img, dx, fc, fc)
        temp2 = (temp1 - img + 128)
        temp3 = cv2.GaussianBlur(temp2, (2 * value2 - 1, 2 * value2 - 1), 0, 0)
        temp4 = img + 2 * temp3 - 255
        dst = np.uint8(img * ((100 - p) / 100) + temp4 * (p / 100))


        imgskin_c = np.uint8(-(self.imgskin - 1))

        dst = np.uint8(dst * self.imgskin + img * imgskin_c)
        self.current_img = dst

# 美白算法(皮肤识别)
    def whitening_skin(self, value=30):
        # value = 30
        value = self.ui.horizontalSlider_13.value()
        img = self.current_img
        imgw = np.zeros(img.shape, dtype='uint8')
        imgw = img.copy()
        midtones_add = np.zeros(256)

        for i in range(256):
            midtones_add[i] = 0.667 * (1 - ((i - 127.0) / 127) * ((i - 127.0) / 127))

        lookup = np.zeros(256, dtype="uint8")

        for i in range(256):
            red = i
            red += np.uint8(value * midtones_add[red])
            red = max(0, min(0xff, red))
            lookup[i] = np.uint8(red)



        rows, cols, channals = img.shape
        for r in range(rows):
            for c in range(cols):

                if self.imgskin[r, c, 0] == 1:
                    imgw[r, c, 0] = lookup[imgw[r, c, 0]]
                    imgw[r, c, 1] = lookup[imgw[r, c, 1]]
                    imgw[r, c, 2] = lookup[imgw[r, c, 2]]
        self.current_img = imgw

# 美白算法(人脸识别)
    def whitening_face(self, value=30):
        # value = 30
        value = self.ui.horizontalSlider_8.value()
        img = self.current_img
        imgw = np.zeros(img.shape, dtype='uint8')
        imgw = img.copy()
        midtones_add = np.zeros(256)

        for i in range(256):
            midtones_add[i] = 0.667 * (1 - ((i - 127.0) / 127) * ((i - 127.0) / 127))

        lookup = np.zeros(256, dtype="uint8")

        for i in range(256):
            red = i
            red += np.uint8(value * midtones_add[red])
            red = max(0, min(0xff, red))
            lookup[i] = np.uint8(red)

        # faces可全局变量
        faces = self.faces

        if faces == ():
            rows, cols, channals = img.shape
            for r in range(rows):
                for c in range(cols):
                    imgw[r, c, 0] = lookup[imgw[r, c, 0]]
                    imgw[r, c, 1] = lookup[imgw[r, c, 1]]
                    imgw[r, c, 2] = lookup[imgw[r, c, 2]]

        else:
            x, y, w, h = faces[0]
            rows, cols, channals = img.shape
            x = max(x - (w * np.sqrt(2) - w) / 2, 0)
            y = max(y - (h * np.sqrt(2) - h) / 2, 0)
            w = w * np.sqrt(2)
            h = h * np.sqrt(2)
            rows = min(rows, y + h)
            cols = min(cols, x + w)
            for r in range(int(y), int(rows)):
                for c in range(int(x), int(cols)):
                    imgw[r, c, 0] = lookup[imgw[r, c, 0]]
                    imgw[r, c, 1] = lookup[imgw[r, c, 1]]
                    imgw[r, c, 2] = lookup[imgw[r, c, 2]]

            processWidth = int(max(min(rows - y, cols - 1) / 8, 2))
            for i in range(1, processWidth):
                alpha = (i - 1) / processWidth
                for r in range(int(y), int(rows)):
                    imgw[r, int(x) + i - 1] = np.uint8(
                        imgw[r, int(x) + i - 1] * alpha + img[r, int(x) + i - 1] * (1 - alpha))
                    imgw[r, int(cols) - i] = np.uint8(
                        imgw[r, int(cols) - i] * alpha + img[r, int(cols) - i] * (1 - alpha))
                for c in range(int(x) + processWidth, int(cols) - processWidth):
                    imgw[int(y) + i - 1, c] = np.uint8(
                        imgw[int(y) + i - 1, c] * alpha + img[int(y) + i - 1, c] * (1 - alpha))
                    imgw[int(rows) - i, c] = np.uint8(
                        imgw[int(rows) - i, c] * alpha + img[int(rows) - i, c] * (1 - alpha))
        self.current_img = imgw

# Gamma矫正
    def gamma_trans(self):
        gamma = (self.ui.horizontalSlider_5.value() + 10) / 10
        img = self.current_img
        gamma_table = [np.power(x / 255.0, gamma) * 255.0 for x in range(256)]
        gamma_table = np.round(np.array(gamma_table)).astype(np.uint8)
        self.current_img = cv2.LUT(img, gamma_table)
        self.show_image()
        self.show_histogram()

# 响应滑动条的变化
    def slider_change(self):
        if self.raw_image is None:
            return 0

        self.current_img = self.raw_image

        # 伽马变换
        if self.ui.horizontalSlider_5.value() != 0:
            self.gamma_trans()

        # 饱和度
        if self.ui.horizontalSlider.value() != 0:
            self.change_saturation()

        if self.ui.horizontalSlider_2.value() != 0:
            pass

        # 边缘保持
        if self.ui.horizontalSlider_3.value() != 0:
            self.edge_preserve()

        # 亮度
        if self.ui.horizontalSlider_4.value() != 0:
            self.change_darker()

        # 美白(人脸识别)
        if self.ui.horizontalSlider_8.value() != 0:
            self.whitening_face()

        # 美白(皮肤识别)
        if self.ui.horizontalSlider_13.value() != 0:
            self.whitening_skin()


        # 风格化
        if self.ui.horizontalSlider_2.value() != 0:
            self.stylize()

        # 细节增强
        if self.ui.horizontalSlider_6.value() != 0:
            self.detail_enhance()

        # 铅笔画
        if self.ui.horizontalSlider_12.value() != 0:
            self.pencil_color()
        self.show_image()


# 计算人脸识别和皮肤识别的基本参数
    def calculate(self):
        if self.raw_image is None:
            return 0
        if self.calculated is False:
            self.faces = self.detect_face()
            if self.faces != ():
                self.detect_skin()
            self.calculated = True

# 怀旧滤镜
    def reminiscene(self):
        if self.raw_image is None:
            return 0
        if self.ui.horizontalSlider_10.value() == 0:
            self.current_img = self.raw_image
            self.show_image()
            return 0
        img = self.raw_image.copy()
        rows, cols, channals = img.shape
        for r in range(rows):
            for c in range(cols):
                B = img.item(r, c, 0)
                G = img.item(r, c, 1)
                R = img.item(r, c, 2)
                img[r, c, 0] = np.uint8(min(max(0.272 * R + 0.534 * G + 0.131 * B, 0), 255))
                img[r, c, 1] = np.uint8(min(max(0.349 * R + 0.686 * G + 0.168 * B, 0), 255))
                img[r, c, 2] = np.uint8(min(max(0.393 * R + 0.769 * G + 0.189 * B, 0), 255))
        self.current_img = img
        self.show_image()

# 木刻滤镜
    def woodcut(self):
        if self.raw_image is None:
            return 0
        if self.ui.horizontalSlider_9.value() == 0:
            # self.current_img = self.raw_image
            self.show_image()
            return 0
        self.gray_image = cv2.cvtColor(self.raw_image, cv2.COLOR_BGR2GRAY)
        gray = self.gray_image
        value = 70 + self.ui.horizontalSlider_9.value()
        rows, cols = gray.shape
        for r in range(rows):
            for c in range(cols):
                if gray[r, c] > value:
                    gray[r, c] = 255
                else:
                    gray[r, c] = 0
        self.gray_image = gray
        self.show_grayimage()

# 铅笔画(灰度)
    def pencil_gray(self):
        if self.raw_image is None:
            return 0
        if self.ui.horizontalSlider_7.value() == 0:
            # self.current_img = self.raw_image
            self.show_image()
            return 0
        value = self.ui.horizontalSlider_7.value() * 0.05
        dst1_gray, dst1_color = cv2.pencilSketch(self.current_img, sigma_s=50, sigma_r=value, shade_factor=0.04)
        self.gray_image = dst1_gray
        self.show_grayimage()

# 铅笔画(彩色)
    def pencil_color(self):
        if self.raw_image is None:
            return 0
        if self.ui.horizontalSlider_12.value() == 0:
            self.current_img = self.raw_image
            self.show_image()
            return 0
        value = self.ui.horizontalSlider_12.value() * 0.05
        dst1_gray, dst1_color = cv2.pencilSketch(self.current_img, sigma_s=50, sigma_r=value, shade_factor=0.04)
        self.current_img = dst1_color


# 风格化
    def stylize(self):
        if self.raw_image is None:
            return 0
        if self.ui.horizontalSlider_2.value() == 0:
            self.current_img = self.raw_image
            self.show_image()
            return 0
        value = self.ui.horizontalSlider_2.value() * 0.05
        self.current_img = cv2.stylization(self.current_img, sigma_s=50, sigma_r=value)


# 细节增强
    def detail_enhance(self):
        if self.raw_image is None:
            return 0
        if self.ui.horizontalSlider_6.value() == 0:
            self.current_img = self.raw_image
            self.show_image()
            return 0
        value = self.ui.horizontalSlider_6.value() * 0.05
        self.current_img = cv2.detailEnhance(self.current_img, sigma_s=50, sigma_r=value)

# 边缘保持
    def edge_preserve(self):
        if self.raw_image is None:
            return 0
        if self.ui.horizontalSlider_3.value() == 0:
            self.current_img = self.raw_image
            self.show_image()
            return 0
        value = self.ui.horizontalSlider_3.value() * 0.05
        self.current_img = cv2.edgePreservingFilter(self.current_img, flags=1, sigma_s=50, sigma_r=value)

# 显示摄像照片
    def show_camera(self):
        flag, self.camera_image = self.cap.read()
        show = cv2.resize(self.image, (640, 480))
        show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB)
        showImage = QtGui.QImage(show.data, show.shape[1], show.shape[0], QtGui.QImage.Format_RGB888)
        self.label_show_camera.setPixmap(QtGui.QPixmap.fromImage(showImage))

运行结果

三、在线协助:

如需安装运行环境或远程调试,可点击博主头像,进入个人主页查看博主联系方式,由专业技术人员远程协助!

1)远程安装运行环境,代码调试
2)Visual Studio, Qt, C++, Python编程语言入门指导
3)界面美化
4)软件制作

博主个人主页:https://developer.aliyun.com/profile/expert/rfnzgp3sk3ahc
博主所有文章点这里:https://developer.aliyun.com/profile/expert/rfnzgp3sk3ahc
博主联系方式点这里:https://developer.aliyun.com/profile/expert/rfnzgp3sk3ahc
相关文章
|
25天前
|
数据采集 数据可视化 数据挖掘
R语言与Python:比较两种数据分析工具
R语言和Python是目前最流行的两种数据分析工具。本文将对这两种工具进行比较,包括它们的历史、特点、应用场景、社区支持、学习资源、性能等方面,以帮助读者更好地了解和选择适合自己的数据分析工具。
27 2
|
25天前
|
C语言 开发者 Python
探索Python中的列表推导式:简洁而强大的工具
【10月更文挑战第21天】在Python的世界里,代码的优雅与效率同样重要。列表推导式(List Comprehensions)作为一种强大而简洁的工具,允许开发者通过一行代码完成对列表的复杂操作。本文将深入探讨列表推导式的使用方法、性能考量以及它如何提升代码的可读性和效率。
|
1月前
|
自然语言处理 算法 数据挖掘
探讨如何利用Python中的NLP工具,从被动收集到主动分析文本数据的过程
【10月更文挑战第11天】本文介绍了自然语言处理(NLP)在文本分析中的应用,从被动收集到主动分析的过程。通过Python代码示例,详细展示了文本预处理、特征提取、情感分析和主题建模等关键技术,帮助读者理解如何有效利用NLP工具进行文本数据分析。
47 2
|
1月前
|
测试技术 Python
Python MagicMock: Mock 变量的强大工具
Python MagicMock: Mock 变量的强大工具
|
23天前
|
C语言 Python
探索Python中的列表推导式:简洁而强大的工具
【10月更文挑战第24天】在Python编程的世界中,追求代码的简洁性和可读性是永恒的主题。列表推导式(List Comprehensions)作为Python语言的一个特色功能,提供了一种优雅且高效的方法来创建和处理列表。本文将深入探讨列表推导式的使用场景、语法结构以及如何通过它简化日常编程任务。
|
1月前
|
机器学习/深度学习 Unix 开发者
python的环境管理工具有哪些
python的环境管理工具有哪些
23 0
|
1月前
|
程序员 开发者 Python
深度解析Python中的元编程:从装饰器到自定义类创建工具
【10月更文挑战第5天】在现代软件开发中,元编程是一种高级技术,它允许程序员编写能够生成或修改其他程序的代码。这使得开发者可以更灵活地控制和扩展他们的应用逻辑。Python作为一种动态类型语言,提供了丰富的元编程特性,如装饰器、元类以及动态函数和类的创建等。本文将深入探讨这些特性,并通过具体的代码示例来展示如何有效地利用它们。
38 0
|
Linux C语言 开发者
源码安装Python学会有用还能装逼 | 解决各种坑
相信朋友们都看过这个零基础学习Python的开篇了
448 0
源码安装Python学会有用还能装逼 | 解决各种坑
下一篇
无影云桌面