#在终端输入
pyuic5 -o my_ui.py my_ui.ui
#利用pyui5工具进行变化,生成py在ui所在文件夹
主要程序代码:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.1
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5.QtWidgets import *
import cv2
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import PyQt5.sip
class Ui_MainWindow(object):
def __init__(self):
self.image_height = 0
self.image_width = 0
self.file_name = ""
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.file = QtWidgets.QLineEdit(self.centralwidget)
self.file.setGeometry(QtCore.QRect(250, 140, 261, 31))
self.file.setObjectName("file_path")
self.open = QtWidgets.QPushButton(self.centralwidget)
self.open.setGeometry(QtCore.QRect(530, 140, 94, 27))
self.open.setObjectName("open")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(160, 150, 72, 19))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(160, 210, 72, 19))
self.label_2.setObjectName("label_2")
self.save = QtWidgets.QPushButton(self.centralwidget)
self.save.setGeometry(QtCore.QRect(320, 280, 121, 41))
self.save.setObjectName("save")
self.horizontalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(250, 200, 324, 41))
self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.size_1 = QtWidgets.QRadioButton(self.horizontalLayoutWidget)
self.size_1.setObjectName("size_1")
self.horizontalLayout.addWidget(self.size_1)
self.size_2 = QtWidgets.QRadioButton(self.horizontalLayoutWidget)
self.size_2.setObjectName("size_2")
self.horizontalLayout.addWidget(self.size_2)
self.size_3 = QtWidgets.QRadioButton(self.horizontalLayoutWidget)
self.size_3.setObjectName("size_3")
self.horizontalLayout.addWidget(self.size_3)
self.size_4 = QtWidgets.QRadioButton(self.horizontalLayoutWidget)
self.size_4.setObjectName("size_4")
self.horizontalLayout.addWidget(self.size_4)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 29))
self.menubar.setObjectName("menubar")
self.menu = QtWidgets.QMenu(self.menubar)
self.menu.setObjectName("menu")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.action1 = QtWidgets.QAction(MainWindow)
self.action1.setObjectName("action1")
self.menu.addAction(self.action1)
self.menubar.addAction(self.menu.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.open.clicked.connect(self.choose_file) # 按钮信号与功能(槽)连接
self.save.clicked.connect(self.resize_save)
self.size_1.toggled.connect(lambda: self.btnstate(self.size_1)) # 选择尺寸按钮信号与槽连接
self.size_2.toggled.connect(lambda: self.btnstate(self.size_2))
self.size_3.toggled.connect(lambda: self.btnstate(self.size_3))
self.size_4.toggled.connect(lambda: self.btnstate(self.size_4))
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "证件照尺寸转换"))
self.open.setText(_translate("MainWindow", "浏览"))
self.label.setText(_translate("MainWindow", "选择图片"))
self.label_2.setText(_translate("MainWindow", "转换类型"))
self.save.setText(_translate("MainWindow", "转换并保存"))
self.size_1.setText(_translate("MainWindow", "二寸"))
self.size_2.setText(_translate("MainWindow", "一寸"))
self.size_3.setText(_translate("MainWindow", "大一寸"))
self.size_4.setText(_translate("MainWindow", "小一寸"))
self.menu.setTitle(_translate("MainWindow", "设置"))
self.action1.setText(_translate("MainWindow", "1"))
def choose_file(self):
self.file_name, _ = QFileDialog.getOpenFileName(None, "choose",
"D:\\",
"JPEG Files(*.jpg);;PNG Files(*.png);;PGM Files(*.pgm)")
self.file.setText(self.file_name)
# print(file_name)
def btnstate(self, btn):
# 大一寸 390*567
# 小一寸 260*378
# 一寸 295*413
# 二寸 413*579
if btn.text() == "二寸":
if btn.isChecked():
self.image_height = 579 # 0.7132
self.image_width = 413
# print(self.image_width / self.image_height)
if btn.text() == "一寸":
if btn.isChecked():
self.image_height = 413 # 0.7142
self.image_width = 295
# print(self.image_width / self.image_height)
if btn.text() == "小一寸":
if btn.isChecked():
self.image_height = 378 # 0.6878
self.image_width = 260
# (self.image_width / self.image_height)
if btn.text() == "大一寸":
if btn.isChecked():
self.image_height = 567 # 0.6878
self.image_width = 390
# print(self.image_width / self.image_height)
def resize_save(self):
if self.file.text() == "":
QtWidgets.QMessageBox.critical(None, "错误", "请选择需转换的图像") # 若不符合要求,弹窗报错
if self.image_height == 0 or self.image_width == 0:
QtWidgets.QMessageBox.critical(None, "错误", "请选择转换的尺寸")
image = cv2.imread(self.file_name, 1) # 读取图片
w = image.shape[1] # 原图像宽
h = image.shape[0] # 原图像高
# 图像比例计算
if (w / h) > (self.image_width / self.image_height):
c = (w / h) - (self.image_width / self.image_height)
c = int((c * h) / 2)
dst = image[0:h, c:(w - c)] # 裁剪掉多出的宽,压缩宽度,避免变形
else:
c = (h / w) - (self.image_height / self.image_width)
c = int((c * w) / 2)
dst = image[c:(h - c), 0:w] # 裁剪掉多出的高,压缩高度,避免变形
result = cv2.resize(dst, (self.image_width, self.image_height)) # 尺寸变换
cv2.imwrite("D:\\01.jpg", result) # 保存转换后的图像
def main():
# 以下为PYQT的UI显示基础命令
# 创建QApplication类的实例
app = QApplication(sys.argv)
widgets = QMainWindow()
window = Ui_MainWindow()
window.setupUi(widgets)
widgets.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
ps: 本次界面较为简陋,有待美化!
有待解决: 在win10中,对于含有中文路径的图像,会出现无法读取、卡死并异常退出的现象,