猫史档案馆


【Python作品分享】process_img【作品秀】

用户:YYY2019YYY2019查看:0 回复:1 评论:0 创建时间:2022-05-15T21:12:16


【作品展示】

center_image

 

【作品介绍】

这是一个很棒的作品

 

【作品源代码】

import sys
from PySide2.QtWidgets import *
from PySide2.QtGui import *
from PIL import Image,ImageFilter,ImageQt
from ui_process_img import Ui_process_img
from sketch import to_sketch
from char import to_char

class ProcessImg(QMainWindow,Ui_process_img):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        
        #图片文件路径
        self.file=None
        self.img_temp=None
        #滤镜
        self.filters={
            "模糊滤镜":ImageFilter.BLUR,
            "轮廓滤镜":ImageFilter.CONTOUR,
            "边缘增强滤镜":ImageFilter.EDGE_ENHANCE,
            "浮雕滤镜":ImageFilter.EMBOSS,
            "锐化滤镜":ImageFilter.SHARPEN,
            "平滑滤镜":ImageFilter.SMOOTH,
        }

        #按钮信号
        self.pbtn_file.clicked.connect(self.select_file)
        self.pbtn_process.clicked.connect(self.process_img)
        self.pbtn_save_img.clicked.connect(self.save_img)

        #标签内图片大小自适应
        self.lb_input_img.setScaledContents(True)
        self.lb_output_img.setScaledContents(True)

        self.show()

    def select_file(self):
        file_path=QFileDialog.getOpenFileName(caption="选择文件",filter="(*.jpg *.png)")
        if file_path[0]:
            self.file=file_path[0]
            img_pix=QPixmap(self.file)
            self.lb_input_img.setPixmap(img_pix)
            
        else:
            QMessageBox.warning(self,"注意","请重新选择图片文件")

    def process_img(self):
        if self.file:
            img=Image.open(self.file)
            text=self.cb_filter_list.currentText()
            if "滤镜" in text:
                self.img_temp=img.filter(self.filters[text]).convert("RGBA")
            elif text=="素描风格转化":
                self.img_temp=to_sketch(img)
            elif text=="字符画风格转化":
                self.img_temp=to_char(img)

            img_qt=ImageQt.ImageQt(self.img_temp)
            img_pix=QPixmap.fromImage(img_qt)
            self.lb_output_img.setPixmap(img_pix)
        else:
            QMessageBox.warning(self,"注意","请先选择图片文件")

    def save_img(self):
        if self.img_temp:
            file_path=QFileDialog.getSaveFileName(caption="保存文件",filter="(*.png)")
            if file_path[0]:
                save_path=file_path[0]
                self.img_temp.save(save_path)
            else:
                QMessageBox.warning(self,"注意","请重新选择保存位置")
        else:
            QMessageBox.warning(self,"注意","请先进行图像处理")

if __name__=="__main__":
    app=QApplication(sys.argv)
    window=ProcessImg()
    sys.exit(app.exec_())



 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
YYY2019YYY2019

这上面展示的是边缘增强滤镜效果,还有其他很多效果,快来试试吧!作品还自带保存功能哦!

点赞0


评论