Python+QT美颜工具源码

news2024/10/2 6:33:46

 程序示例精选

Python+QT美颜工具源码

如需安装运行环境或远程调试,见文章底部微信名片,由专业技术人员远程协助!

前言

这篇博客针对《Python+QT美颜》编写代码,功能包括了亮度,磨皮,风格化,铅笔化等多种特征修饰,代码整洁,规则,易读。 学习与应用推荐首选。


文章目录

        一、所需工具软件

        二、使用步骤

                1. 引入库

                2. 显示图片

                3. 美颜特征函数定义

                4. 运行结果

         三在线协助


一、所需工具软件

          1. Python3.6以上

          2. Pycharm代码编辑器

          3. PyQt, OpenCV库

二、使用步骤

1.引入库

代码如下(示例):

from PyQt5 import Qt
from PyQt5 import QtCore,QtWidgets,QtGui
import sys
import PyQt5
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QFileDialog, QGraphicsRectItem, QGraphicsScene
from PyQt5.QtCore import QSize
import cv2
import numpy as np
from matplotlib import pyplot as plt

2.显示图片

代码如下(示例):

# 显示图片
    def show_image(self):
        img_cv = cv2.cvtColor(self.current_img, cv2.COLOR_RGB2BGR)
        img_width, img_height, a = img_cv.shape
        ratio_img = img_width/img_height
        ratio_scene = self.ui.graphicsView.width()/self.ui.graphicsView.height()
        if ratio_img > ratio_scene:
            width = int(self.ui.graphicsView.width())
            height = int(self.ui.graphicsView.width() / ratio_img)
        else:
            width = int(self.ui.graphicsView.height() * ratio_img)
            height = int(self.ui.graphicsView.height())
        img_resize = cv2.resize(img_cv, (height-5, width-5), interpolation=cv2.INTER_AREA)
        h, w, c = img_resize.shape
        bytesPerLine = w * 3
        qimg = QImage(img_resize.data, w, h, bytesPerLine, QImage.Format_RGB888)
        self.scene = QGraphicsScene()
        pix = QPixmap(qimg)
        self.scene.addPixmap(pix)
        self.ui.graphicsView.setScene(self.scene)

3.美颜特征函数定义:

代码如下(示例):

# 饱和度
    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))

4.运行结果如下: 

三、在线协助: 

如需安装运行环境或远程调试,见文章底部微信名片,由专业技术人员远程协助!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/95507.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

软件测试人员一定要会的微信小程序测试点

微信小程序&#xff1a; 不需要下载安装即可使用的应用&#xff0c; 实现了应用“触手可及”的梦想&#xff0c;用户扫一扫或者搜一下即可打开应用。 体现了“用完即走”的理念&#xff0c;用户不用关心是否安装太多应用的问题。 应用将无处不在&#xff0c;随时可用&#…

Mysql分布式锁(一)通过jvm本地锁解决mysql并发问题及可能的失效情况

强烈建议配合之前的JVM本地锁&#xff08;一&#xff09;简单实现阅读 mysql场景 将之前的场景修改为mysql场景&#xff0c;即在数据库中保存一条数据&#xff0c;多个线程并发处理该数据。 数据库建表如下 pom.xml中新增mybatis-plus和mysql <dependency><groupId…

戴尔r730xd服务器从u盘启动设置方法(戴尔r730取消网络启动方法)

1,开机后出现提示的时候&#xff0c;按F12 2,等一会系统会自动进入BIOS选择菜单:选择system bios 回车 3,这时在选择boot setting 回车: 4,在这里选择 BIOS Boot settings 5,将网卡启动的勾选去掉&#xff0c;即默认使用C盘启动 6,退出Esc&#xff0c;会提示保存&#xff0…

C#语言实例源码系列-电脑系统挂机锁

专栏分享点击跳转>Unity3D特效百例点击跳转>案例项目实战源码点击跳转>游戏脚本-辅助自动化点击跳转>Android控件全解手册 &#x1f449;关于作者 众所周知&#xff0c;人生是一个漫长的流程&#xff0c;不断克服困难&#xff0c;不断反思前进的过程。在这个过程中…

[附源码]Python计算机毕业设计广西助农平台Django(程序+LW)

该项目含有源码、文档、程序、数据库、配套开发软件、软件安装教程 项目运行 环境配置&#xff1a; Pychram社区版 python3.7.7 Mysql5.7 HBuilderXlist pipNavicat11Djangonodejs。 项目技术&#xff1a; django python Vue 等等组成&#xff0c;B/S模式 pychram管理等…

微服务框架 SpringCloud微服务架构 多级缓存 46 JVM 进程缓存 46.2 导入商品案例【导入Demo工程】

微服务框架 【SpringCloudRabbitMQDockerRedis搜索分布式&#xff0c;系统详解springcloud微服务技术栈课程|黑马程序员Java微服务】 多级缓存 文章目录微服务框架多级缓存46 JVM 进程缓存46.2 导入商品案例【导入Demo工程】46.2.1 导入商品管理案例demo工程46 JVM 进程缓存 …

你知道吗?Python原来可以做这些

如果你准备开始学习Python或者你已经开始了学习Python&#xff0c;那么&#xff0c;你肯能会问自己&#xff1a; “我用Python究竟能做些什么&#xff1f;” 嗯&#xff0c;这是一个棘手的问题&#xff0c;因为Python有很多应用方向。但随着时间的推移&#xff0c;我发现Pytho…

Java图书管理代码

Java图书管理代码一&#xff1a;简介二&#xff1a;核心需求三: 类的设计1. 创建图书相关的类2. 创建操作相关的类3. 创建用户相关的类4. 进行整合5. 实现具体的每个 Operation大家好&#xff0c;我是晓星航。今天为大家带来的是Java语言图书馆里系统的相关的讲解&#xff01;&…

【裂缝识别】无人机裂缝图像处理系统(带面板)【含GUI Matlab源码 1727期】

⛄一、简介 1 案例背景 随着国家对公路建设的大力投入&#xff0c;我国的公路通车总里程己经位居世界前列&#xff0c;这样进一步促进了我国经济建设的发展。随着公路的大量投运&#xff0c;公路日常养护和管理已经成为制约公路运营水平提高的瓶颈&#xff0c;特别是路面状态采…

使用QtDesignerForm界面

使用QtDesignerForm界面1. 概述2. UI文件的使用分析2.2 QtDesignerForm 界面的使用3. Demo1. 概述 在进行QT开发创建UI文件通常有两种方式&#xff0c;分别是通过Qt 设计师界面类、Qt Designer Form创建。两者创建的区别如下。 通过Qt 设计师界面类创建的界面通常会包括对应的…

手把手系列:如何将小程序游戏引入自有APP?(iOS篇)

自FinClip 诞生以来&#xff0c;一直有不少开发者询问官方 FinClip 什么时候可以支持微信小游戏&#xff1f;实际上&#xff0c;从去年开始我们就把支持微信小游戏的计划做进了产品的 Roadmap。2022年底&#xff0c;我们终于在新年之前实现了对小游戏的支持&#xff01; 近期…

中文输入法中光标跟随能力触发的浏览器事件探究

:::tip 最近在着手腾讯文档的输入体验优化&#xff0c;在其中有一个不起眼的小需求引起了我的注意&#xff0c;并顺便研究了一些事件监听机制相结合的特点&#xff0c;特此记录一下填坑过程。 ::: 模拟光标跟随 大部分的主流输入法都有这样一个特性&#xff0c;在输入中文时&…

昆仑天工开源的AIGC

🍿*★,*:.☆欢迎您/$:*.★* 🍿 https://github.com/SkyWorkAIGC/SkyCode 技术优势一 :涵盖多种编程语言 不同的编程语言着重于解决不同平台、环境下的问题,不同的编程语言都有自己存在的理由。奇点智源SkyCode能够生成的代码,不仅包括使用广泛的JavaScript、python、Jav…

(六)springcloud之Nacos集群与持久化配置-3

模块&#xff1a; 1.父工程&#xff08;管理版本&#xff09; 2.公共模块&#xff1a;Common-API 3.测试模块&#xff1a;NacosClusterConfigConsumer80 版本&#xff1a; springboot:2.7.6 springcloud:2021.0.5 spring-cloud-alibaba-dependencies:2021.0.1.0 nacos:2.1.2 ng…

RCFSNet

搬来了一种结合道路上下文信息与多尺度特征的道路遥感图像道路提取方法&#xff0c;与常见的道路提取算法相比&#xff0c;RCFSNet能够获取完整的路网标签&#xff0c;在遮挡场景中表现出色 本人是太原理工大学大数据学院在2022年发表在《IEEE Geoscience and remote sensing …

SpringBoot网站开发常用工具类(自己写的适合入门)

目录 字符集转换工具 适用 代码 JWT工具类 适用 代码 七牛云文件上传工具类 适用 代码 文件类型转换工具类 适用 代码 session&#xff0c;cookie工具类 适用 代码 字符集转换工具 适用 主要是完成与前端配合&#xff0c;共同实现特殊字符串传输过程中被转译的问…

数字藏品系统应用场景介绍——元宇宙NFG

数字藏品系统应用场景&#xff1a; 1.虚实结合产品营销&#xff1a;品牌企业限量发行产品&#xff0c;比如限量珍藏版产品&#xff0c;茅台酒&#xff0c;耐克鞋&#xff0c;劳力士手表&#xff0c;爱马仕包包&#xff0c;钻石珠宝&#xff0c;结合元宇宙热点营销&#xff0c;…

Crane 发布国内首个云原生应用碳排放计算优化器

为了共同应对气候变化挑战&#xff0c;减缓全球变暖趋势&#xff0c;2015年12月&#xff0c;近200个缔约方共同通过了《巴黎协定》&#xff08;The Paris Agreement&#xff09;&#xff0c;对2020年后全球如何应对气候变化做出了行动安排。为实现这一目标&#xff0c;全球多个…

我国液化石油气行业SWOT分析:产销规模持续上涨 供需缺口劣势明显

液化石油气是在炼油厂内&#xff0c;由天然气或者石油进行加压降温液化所得到的一种无色挥发性液体,它极易自燃&#xff0c;当其在空气中的含量达到了一定的浓度范围后&#xff0c;它遇到明火就能爆炸。 一、优势分析 根据观研报告网发布的《中国液化石油气市场发展深度分析与…

【Unity】UI ToolKit 学习记录

Unity推出的这个 UI ToolKit&#xff0c;据说是要用来替代UGUI。既然这么有野心&#xff0c;那肯定要搞来看一看。这次使用目标就是用这个 UI ToolKit 生成一堆类似HUD的头标&#xff0c;然后看看使用难易程度和性能如何。 本文对应Unity版本 &#xff1a;2020.3.41f1c1 1、安装…