猫史档案馆


【Python作品分享】process_image

用户:0x4D20x4D2查看:0 回复:0 评论:0 创建时间:2020-11-15T21:25:23


【作品展示】

center_image

 

【作品介绍】

az

 

【作品源代码】

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


class ProcessImg(QMainWindow, Ui_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_2.clicked.connect(self.process_img)
        self.pushButton.clicked.connect(self.save_img)
        self.lb_inpot_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_inpot_img.setPixmap(img_pix)
            self.img_temp = None
        else:
            print(time.strftime("%H:%M:%S"), ":Error code:1  你未选择你的文件!")
            QMessageBox.warning(self, "提示", "你未选择你的文件!")

    def process_img(self):
        if self.img_temp:
            img = self.img_temp
        else:
            if self.file:
                img = Image.open(self.file)
            else:
                print(time.strftime("%H:%M:%S"), ":Error code:1  你未选择你的文件!")
                QMessageBox.warning(self, "提示", "你未选择你的文件!")
                return
        text = self.comboBox.currentText()
        try:
            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)
            print(time.strftime("%H:%M:%S"), ":图片转换完毕")
        except KeyError:
            print(time.strftime("%H:%M:%S"), ":Error Code:3  本选项为空选项,无法转换!")
            QMessageBox.warning(self, "错误", "本选项为空选项,无法转换!")
        except UnboundLocalError:
            pass
        except:
            print(time.strftime("%H:%M:%S"), ":Error Code:0  未知错误")
            QMessageBox.warning(self, "错误", "未知错误")

    def save_img(self):
        file_path = QFileDialog.getSaveFileName(
            caption="选择文件", filter="(*.png)")
        try:
            if file_path[0]:
                save_path = file_path[0]
                self.img_temp.save(save_path)
                print(time.strftime("%H:%M:%S"), "文件保存成功!")
                QMessageBox.warning(self, "恭喜", "文件保存成功!")
            else:
                print(time.strftime("%H:%M:%S"),
                      ":Error Code:4  文件保存路径错误,请重新选择!")
                QMessageBox.warning(self, "错误", "文件保存路径错误,请重新选择!")
        except NameError:
            print(time.strftime("%H:%M:%S"), ":Error Code:4  无文件可保存!")
            QMessageBox.warning(self, "提示", "无文件可保存!")
        except OSError:
            print(time.strftime("%H:%M:%S"),
                  ":Error Code:6  磁盘空间不足,请清理磁盘空间或更换磁盘保存!")
            QMessageBox.warning(self, "警告", "磁盘空间不足,请清理磁盘空间或更换磁盘保存!")


if __name__ == "__main__":
    print("#################################")
    print("#        欢迎使用本软件!       #")
    print("#          图片转换器           #")
    print("#          version 1.2          #")
    print("#################################")
    print("bilibili账号喵4785952喵")
    print("求关注!")
    app = QApplication(sys.argv)
    print("加载中......")
    time.sleep(0.6)
    window = ProcessImg()
    print("\n—————————————日志———————————————")
    print(time.ctime(), ":程序开始运行")
    sys.exit(app.exec_())

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页