简易Ps的Python实现

完整代码

#-*- coding: utf-8 -*-
"""
Created on Sat May  2 10:09:55 2020
@author: 久未
"""
import sys,win32ui,os
from PIL import Image,ImageQt,ImageDraw,ImageFont
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class ps(QWidget):
    def __init__(self):
        """界面初始化"""
        super(ps, self).__init__()
        self.UI_init()
        
    def UI_init(self):
        '''GUI板块设置'''
        #顶层窗口大小设置
        desktop = QApplication.desktop()
        screenRect = desktop.screenGeometry()
        win_height = screenRect.height()
        win_width = screenRect.width()
        self.resize(win_width,win_height)
        self.setWindowTitle('PS Made By 周逸彪')
        #交互区域设置
        #图片区设置
        x=0.9
        self.pic_label = QLabel(self)
        self.pic_label.setFixedSize(x*win_width,0.90*win_height)
        self.pic_label.move(0,0)# 0.1*win_height
        self.pic_label.setStyleSheet("QLabel{background:none;}")
        self.pic_label.setFrameShape(QtWidgets.QFrame.Box)
        self.pic_label.setLineWidth(1)
        
        #按钮区背景,分为图片打开按钮,亮度调节按钮,缩放区,旋转区,加注水印区等
        self.back = QLabel(self)
        y=0.8
        self.back.setFixedSize(0.9*(1-x)*win_width,y*win_height)
        self.back.move(x*win_width+0.05*(1-x)*win_width,0.3*(1-y)*win_height)
        self.back.setStyleSheet("QLabel{background:none;}")
        # 开始:
        wlayout = QtWidgets.QVBoxLayout() # 按钮区全局布局
        
        hlayout1 = QtWidgets.QVBoxLayout() #按钮区局部布局,6个
        hlayout2 = QtWidgets.QVBoxLayout()
        glayout1 = QtWidgets.QGridLayout()
        glayout2 = QtWidgets.QGridLayout()
        hlayout3 = QtWidgets.QVBoxLayout()
        glayout3 = QtWidgets.QGridLayout()
        #细节
        self.打开图片 = QtWidgets.QPushButton("打开图片")
        self.打开图片.clicked.connect(self.openimage)
        self.还原 = QtWidgets.QPushButton("图片还原")
        self.还原.clicked.connect(self.goback)
        self.应用亮度 = QtWidgets.QPushButton('应用亮度')
        self.应用亮度.clicked.connect(self.briApply)
        self.目标亮度=QtWidgets.QLineEdit('目标亮度的倍数')
        self.目标亮度.setAlignment(Qt.AlignCenter)
        self.左旋 = QtWidgets.QPushButton('左旋')
        self.左旋.clicked.connect(self.rightorient)
        self.旋转度数=QtWidgets.QLabel('90度')
        self.旋转度数.setAlignment(Qt.AlignCenter)
        self.左右反转 = QtWidgets.QPushButton('左右翻转')
        self.左右反转.clicked.connect(self.hturnover)
        self.上下反转 = QtWidgets.QPushButton('上下翻转')
        self.上下反转.clicked.connect(self.vturnover)
        
        self.横向 = QtWidgets.QLabel('横向')
        self.纵向 = QtWidgets.QLabel('纵向')
        self.横向比例 = QtWidgets.QLineEdit('1')
        self.纵向比例 = QtWidgets.QLineEdit('1')
        self.应用缩放 = QtWidgets.QPushButton('应用缩放')
        self.应用缩放.clicked.connect(self.sfApply)
        self.纵向比例.setAlignment(Qt.AlignCenter)
        self.横向比例.setAlignment(Qt.AlignCenter)
        self.添加水印 = QtWidgets.QPushButton('添加水印')
        self.添加水印.clicked.connect(self.watermark)
        self.水印内容=QtWidgets.QLineEdit('水印内容')
        self.水印内容.setAlignment(Qt.AlignCenter)
        self.保存 = QtWidgets.QPushButton('点击保存')
        self.保存.clicked.connect(self.save)
        self.名称 = QtWidgets.QLineEdit('未命名')
        self.后缀 = QtWidgets.QLineEdit('.jpg')

        hlayout1.addWidget(self.打开图片)
        hlayout1.addWidget(self.还原) # 局部布局添加部件
        hlayout2.addWidget(self.应用亮度)
        hlayout2.addWidget(self.目标亮度)
        glayout1.addWidget(self.左旋,0,0,1,1)
        glayout1.addWidget(self.旋转度数,0,1,1,1)
        glayout1.addWidget(self.左右反转,1,0,1,1)
        glayout1.addWidget(self.上下反转,1,1,1,1)
        glayout2.addWidget(self.横向,0,0)
        glayout2.addWidget(self.纵向,0,1)
        glayout2.addWidget(self.横向比例,1,0)
        glayout2.addWidget(self.纵向比例,1,1)
        glayout2.addWidget(self.应用缩放,2,0,2,2)
        hlayout3.addWidget(self.添加水印)
        hlayout3.addWidget(self.水印内容)
        glayout3.addWidget(self.名称,0,0)
        glayout3.addWidget(self.后缀,0,1)
        glayout3.addWidget(self.保存,1,0,1,2)
        
        pic_open = QtWidgets.QWidget() # 准备五个部件
        bri = QtWidgets.QWidget()
        orien = QtWidgets.QWidget()
        sf = QtWidgets.QWidget()
        sy= QtWidgets.QWidget()
        save = QtWidgets.QWidget()
        
        pic_open.setLayout(hlayout1) # 五个部件设置局部布局
        bri.setLayout(hlayout2)
        orien.setLayout(glayout1)
        sf.setLayout(glayout2)
        sy.setLayout(hlayout3)
        save.setLayout(glayout3)
        
        wlayout.addWidget(pic_open) # 五个部件加至全局布局
        wlayout.addWidget(bri)
        wlayout.addWidget(orien)
        wlayout.addWidget(sf)
        wlayout.addWidget(sy)
        wlayout.addWidget(save)
        
        self.back.setLayout(wlayout) # 全局布局设置父级是back标签
        
    def openimage(self):
        """打开图片"""
        dlg = win32ui.CreateFileDialog(1)  # 1表示打开文件对话框
        dlg.DoModal()
        self.filename=dlg.GetPathName()  # 获取选择的文件名称
        if self.filename.endswith('.jpg') or self.filename.endswith('.png'):
            #打开Image图片
            self.Img=Image.open(self.filename)
            self.bImage=self.Img.copy()
            self.bImage1=self.Img.copy()
            try:
                for orientation in ExifTags.TAGS.keys() : 
                    if ExifTags.TAGS[orientation]=='Orientation' : break 
                exif=dict(self.Img._getexif().items())
                if exif[orientation] == 3 : 
                    self.Img=self.Img.rotate(180, expand = True)
                elif exif[orientation] == 6 : 
                    self.Img=self.Img.rotate(270, expand = True)
                elif exif[orientation] == 8 : 
                    self.Img=self.Img.rotate(90, expand = True)
            except:
                pass
            self.pixshow()
        else:
            reply=QMessageBox.question(self,"文件打开失败","请选择jpg或者png类型文件",QMessageBox.Yes | QMessageBox.No)
            while reply==QMessageBox.No:
                reply=QMessageBox.question(self,"文件打开失败","请选择jpg或者png类型文件",QMessageBox.Yes | QMessageBox.No)
        
    def pixshow(self):
        '''将处理后的图片进行刷新'''
        # Image转QImage
        self.qim = ImageQt.ImageQt(self.Img)
        #Qimage转Qpixmap
        self.pix=QtGui.QPixmap.fromImage(self.qim).scaled(self.pic_label.width(), self.pic_label.height(),aspectRatioMode=Qt.KeepAspectRatio)
        self.pic_label.setAlignment(Qt.AlignCenter)
        self.pic_label.setPixmap(self.pix)

        
    def briApply(self):
        '''应用亮度'''
        text = self.目标亮度.text()
        self.Img=self.bImage.copy()
        try:
            if text=='目标亮度的倍数':
                t=1
            else:
                t = float(text)
            self.Img = self.Img.point(lambda p: p * t)
        except ValueError: 
            reply=QMessageBox.question(self,"输入格式错误","请输入double类型",QMessageBox.Yes | QMessageBox.No)
            while reply==QMessageBox.No:
                reply=QMessageBox.question(self,"输入格式错误","请输入double类型",QMessageBox.Yes | QMessageBox.No)
        self.pixshow()

    def rightorient(self):
        '''顺时针旋转'''
        self.Img = self.Img.transpose(Image.ROTATE_90)
        self.bImage=self.Img.copy()
        self.pixshow()
    
    def hturnover(self):
        '''水平翻转'''
        self.Img=self.Img.transpose(Image.FLIP_LEFT_RIGHT)
        self.bImage=self.Img.copy()
        self.pixshow()
    
    def vturnover(self):
        '''上下颠倒'''
        self.Img=self.Img.transpose(Image.FLIP_TOP_BOTTOM)
        self.bImage=self.Img.copy()
        self.pixshow()
    
    def sfApply(self):
        '''图片缩放'''
        text1 = self.横向比例.text()
        text2 = self.纵向比例.text()
        self.Img=self.bImage.copy()
        try: 
            t1 = float(text1)
            t2 = float(text2)
            w,h=self.Img.size
            self.Img = self.Img.resize((int(t1*w),int(t2*h)),Image.ANTIALIAS)
        except ValueError: 
            reply=QMessageBox.question(self,"输入格式错误","请输入double类型",QMessageBox.Yes | QMessageBox.No)
            while reply==QMessageBox.No:
                reply=QMessageBox.question(self,"输入格式错误","请输入double类型",QMessageBox.Yes | QMessageBox.No)
        self.pixshow()
    
    def watermark(self):
        '''添加水印'''
        text = self.水印内容.text()
        self.Img=self.bImage.copy()
        try:
            if text=='水印内容':
                text='作者:周逸彪'
            ww,wh=self.Img.size
            textsize=wh//20
            draw = ImageDraw.Draw(self.Img)
            fontpath = "font/STXINGKA.TTF"
            font1 = ImageFont.truetype(fontpath,textsize)
            text_size_x, text_size_y = draw.textsize(text, font=font1)
            draw.text((ww-text_size_x,wh-text_size_y),text,'white',font=font1)
        except ValueError: 
            pass
        self.pixshow()

    def save(self):
        path=os.path.abspath('.')
        path=path.replace("\\","/")
        text1 = self.名称.text()
        text2 = self.后缀.text()
        if text2 in ['.jpg','.png']:
            self.Img.save(path+'/'+text1+text2)
            QMessageBox.information(self,"图片保存成功","图片保存成功!",QMessageBox.Yes | QMessageBox.No)
        else :
            reply=QMessageBox.question(self,"保存后缀错误","后缀从.jpg和.png中选择",QMessageBox.Yes | QMessageBox.No)
            while reply==QMessageBox.No:
                reply=QMessageBox.question(self,"保存后缀错误","后缀从.jpg和.png中选择",QMessageBox.Yes | QMessageBox.No)

    def goback(self):
        self.Img=self.bImage1.copy()
        self.pixshow()
        
if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    my_ps = ps()
    my_ps.show()
    sys.exit(app.exec_())

效果图

简易Ps的Python实现_第1张图片

你可能感兴趣的:(细枝末节,python)