123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- # -*- coding: utf-8 -*-
- import time
- import sys
- import os
- from UI.resize_photos import Ui_Form as ResizePicUI
- from PySide6.QtWidgets import *
- from PySide6.QtCore import *
- from PIL import Image
- from .MineQWidget import MineQWidget
- class ResizePic(MineQWidget, ResizePicUI):
- signal_data = Signal(dict)
- def __init__(self, windows=None):
- super().__init__()
- self.windows = windows
- self.setupUi(self)
- self.setFixedSize(self.width(), self.height())
- self.allow_modified = True
- self.pic_width_target = 10000
- self.state = 0
- self.show_img_dir = ""
- self.change_state(self.state)
- self.init()
- self.setWindowModality(Qt.ApplicationModal)
- self.show()
- def change_state(self, index=0):
- if index == 0:
- self.progressBar.hide()
- self.label_5.hide()
- self.pushButton.setText("执行压缩")
- self.pushButton.setEnabled(True)
- if index == 1:
- self.progressBar.show()
- self.progressBar.setValue(0)
- self.textBrowser_2.setText("")
- self.label_5.show()
- self.pushButton.setText("进行中")
- self.pushButton.setEnabled(False)
- def init(self):
- self.label_4.setText("")
- self.label_8.hide()
- self.label_3.mousePressEvent = self.change_img_dir
- self.label_8.mousePressEvent = self.open_dir
- self.signal_data.connect(self.show_info)
- self.pushButton.clicked.connect(self.run)
- def open_dir(self, *args, **kwargs):
- if self.show_img_dir:
- os.startfile(self.show_img_dir)
- def run(self, is_sure=False):
- if not self.lineEdit.text():
- self.WaringMessage("请输入目标像素")
- return
- else:
- try:
- self.pic_width_target = int(self.lineEdit.text())
- if self.pic_width_target < 200 or self.pic_width_target > 2000:
- self.WaringMessage("建议像素在800px左右")
- return
- except:
- self.WaringMessage("像素必须是整数")
- return
- if not is_sure:
- if not self.show_img_dir:
- self.WaringMessage("请选择图片文件夹")
- return
- a = self.comfirmMessage(
- "是否确认继续执行,软件会自动处理所有子文件夹,请自行备份数据,执行后无法恢复"
- )
- if a != QMessageBox.Yes:
- return
- # 计算待处理图片数量
- total_pic = 0
- _Type = ['.png', '.PNG', '.jpg', '.JPG', '.gif', '.GIF', '.jpeg', '.JPEG']
- for dirpath, dirnames, filenames in os.walk(self.show_img_dir):
- for file in filenames:
- if os.path.splitext(file)[1] in _Type:
- total_pic += 1
- if not total_pic:
- self.WaringMessage("没有任何图片")
- return
- a = self.comfirmMessage("待处理图片数:{},是否继续".format(total_pic))
- if a == QMessageBox.Yes:
- self.run(is_sure=True)
- else:
- self.deal_pic()
- def show_info(self, data):
- if data["_type"] == "show_schedule":
- self.progressBar.setValue(data["data"])
- if data["_type"] == "show_text":
- self.textBrowser_2.append(data["data"])
- if data["_type"] == "change_state":
- self.change_state(data["data"])
- def deal_pic(self):
- total_pic = 0
- _Type = ['.png', '.PNG', '.jpg', '.JPG', '.gif', '.GIF', '.jpeg', '.JPEG']
- for dirpath, dirnames, filenames in os.walk(self.show_img_dir):
- for file in filenames:
- if os.path.splitext(file)[1] in _Type:
- total_pic += 1
- self.signal_data.emit({"_type": "change_state",
- "data": 1})
- self.signal_data.emit({"_type": "show_schedule",
- "data": 0})
- n = 0
- _Type = ['.png', '.PNG', '.jpg', '.JPG', '.gif', '.GIF', '.jpeg', '.JPEG']
- for dirpath, dirnames, filenames in os.walk(self.show_img_dir):
- for file in filenames:
- if os.path.splitext(file)[1] in _Type:
- n += 1
- file_path = dirpath + "/" + file
- self.signal_data.emit({"_type": "show_schedule",
- "data": (n / total_pic * 100)})
- self.signal_data.emit({"_type": "show_text",
- "data": "正在处理:{}".format(file)})
- print(file_path)
- pic = Picture(file_path)
- if pic.x > self.pic_width_target:
- pic.resize(self.pic_width_target)
- pic.save_img(file_path)
- self.signal_data.emit({"_type": "show_text",
- "data": "已完成"})
- self.signal_data.emit({"_type": "change_state",
- "data": 0})
- def change_img_dir(self, *args):
- folder = QFileDialog.getExistingDirectory(self, "选取文件夹", "./")
- print(folder)
- self.show_img_dir = folder
- self.label_4.setText(self.show_img_dir)
- if self.show_img_dir:
- self.label_8.show()
- else:
- self.label_8.hide()
- class Picture:
- def __init__(self, in_path):
- self.im = Image.open(in_path)
- self.x, self.y = self.im.size
- # print(self.x, self.y)
- def save_img(self, outpath, quality=90):
- if outpath.endswith("jpg"):
- if self.im.mode != 'RGB':
- self.im = self.im.convert("RGB")
- self.im.save(outpath,format="JPEG")
- return
- if outpath.endswith("png"):
- if self.im.mode != 'RGBA':
- self.im = self.im.convert("RGBA")
- self.im.save(outpath)
- return
- self.im.save(outpath)
- def resize(self, width):
- re_x = int(width)
- re_y = int(self.y * re_x / self.x)
- self.im = self.im.resize((re_x, re_y), Image.Resampling.LANCZOS)
- self.x, self.y = self.im.size
- def resize_regular(self, width, high):
- self.im = self.im.resize((width, high), Image.Resampling.LANCZOS)
|