module_resize_pic.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # -*- coding: utf-8 -*-
  2. import time
  3. import sys
  4. import os
  5. from UI.resize_photos import Ui_Form as ResizePicUI
  6. from PySide6.QtWidgets import *
  7. from PySide6.QtCore import *
  8. from PIL import Image
  9. from .MineQWidget import MineQWidget
  10. class ResizePic(MineQWidget, ResizePicUI):
  11. signal_data = Signal(dict)
  12. def __init__(self, windows=None):
  13. super().__init__()
  14. self.windows = windows
  15. self.setupUi(self)
  16. self.setFixedSize(self.width(), self.height())
  17. self.allow_modified = True
  18. self.pic_width_target = 10000
  19. self.state = 0
  20. self.show_img_dir = ""
  21. self.change_state(self.state)
  22. self.init()
  23. self.setWindowModality(Qt.ApplicationModal)
  24. self.show()
  25. def change_state(self, index=0):
  26. if index == 0:
  27. self.progressBar.hide()
  28. self.label_5.hide()
  29. self.pushButton.setText("执行压缩")
  30. self.pushButton.setEnabled(True)
  31. if index == 1:
  32. self.progressBar.show()
  33. self.progressBar.setValue(0)
  34. self.textBrowser_2.setText("")
  35. self.label_5.show()
  36. self.pushButton.setText("进行中")
  37. self.pushButton.setEnabled(False)
  38. def init(self):
  39. self.label_4.setText("")
  40. self.label_8.hide()
  41. self.label_3.mousePressEvent = self.change_img_dir
  42. self.label_8.mousePressEvent = self.open_dir
  43. self.signal_data.connect(self.show_info)
  44. self.pushButton.clicked.connect(self.run)
  45. def open_dir(self, *args, **kwargs):
  46. if self.show_img_dir:
  47. os.startfile(self.show_img_dir)
  48. def run(self, is_sure=False):
  49. if not self.lineEdit.text():
  50. self.WaringMessage("请输入目标像素")
  51. return
  52. else:
  53. try:
  54. self.pic_width_target = int(self.lineEdit.text())
  55. if self.pic_width_target < 200 or self.pic_width_target > 2000:
  56. self.WaringMessage("建议像素在800px左右")
  57. return
  58. except:
  59. self.WaringMessage("像素必须是整数")
  60. return
  61. if not is_sure:
  62. if not self.show_img_dir:
  63. self.WaringMessage("请选择图片文件夹")
  64. return
  65. a = self.comfirmMessage(
  66. "是否确认继续执行,软件会自动处理所有子文件夹,请自行备份数据,执行后无法恢复"
  67. )
  68. if a != QMessageBox.Yes:
  69. return
  70. # 计算待处理图片数量
  71. total_pic = 0
  72. _Type = ['.png', '.PNG', '.jpg', '.JPG', '.gif', '.GIF', '.jpeg', '.JPEG']
  73. for dirpath, dirnames, filenames in os.walk(self.show_img_dir):
  74. for file in filenames:
  75. if os.path.splitext(file)[1] in _Type:
  76. total_pic += 1
  77. if not total_pic:
  78. self.WaringMessage("没有任何图片")
  79. return
  80. a = self.comfirmMessage("待处理图片数:{},是否继续".format(total_pic))
  81. if a == QMessageBox.Yes:
  82. self.run(is_sure=True)
  83. else:
  84. self.deal_pic()
  85. def show_info(self, data):
  86. if data["_type"] == "show_schedule":
  87. self.progressBar.setValue(data["data"])
  88. if data["_type"] == "show_text":
  89. self.textBrowser_2.append(data["data"])
  90. if data["_type"] == "change_state":
  91. self.change_state(data["data"])
  92. def deal_pic(self):
  93. total_pic = 0
  94. _Type = ['.png', '.PNG', '.jpg', '.JPG', '.gif', '.GIF', '.jpeg', '.JPEG']
  95. for dirpath, dirnames, filenames in os.walk(self.show_img_dir):
  96. for file in filenames:
  97. if os.path.splitext(file)[1] in _Type:
  98. total_pic += 1
  99. self.signal_data.emit({"_type": "change_state",
  100. "data": 1})
  101. self.signal_data.emit({"_type": "show_schedule",
  102. "data": 0})
  103. n = 0
  104. _Type = ['.png', '.PNG', '.jpg', '.JPG', '.gif', '.GIF', '.jpeg', '.JPEG']
  105. for dirpath, dirnames, filenames in os.walk(self.show_img_dir):
  106. for file in filenames:
  107. if os.path.splitext(file)[1] in _Type:
  108. n += 1
  109. file_path = dirpath + "/" + file
  110. self.signal_data.emit({"_type": "show_schedule",
  111. "data": (n / total_pic * 100)})
  112. self.signal_data.emit({"_type": "show_text",
  113. "data": "正在处理:{}".format(file)})
  114. print(file_path)
  115. pic = Picture(file_path)
  116. if pic.x > self.pic_width_target:
  117. pic.resize(self.pic_width_target)
  118. pic.save_img(file_path)
  119. self.signal_data.emit({"_type": "show_text",
  120. "data": "已完成"})
  121. self.signal_data.emit({"_type": "change_state",
  122. "data": 0})
  123. def change_img_dir(self, *args):
  124. folder = QFileDialog.getExistingDirectory(self, "选取文件夹", "./")
  125. print(folder)
  126. self.show_img_dir = folder
  127. self.label_4.setText(self.show_img_dir)
  128. if self.show_img_dir:
  129. self.label_8.show()
  130. else:
  131. self.label_8.hide()
  132. class Picture:
  133. def __init__(self, in_path):
  134. self.im = Image.open(in_path)
  135. self.x, self.y = self.im.size
  136. # print(self.x, self.y)
  137. def save_img(self, outpath, quality=90):
  138. if outpath.endswith("jpg"):
  139. if self.im.mode != 'RGB':
  140. self.im = self.im.convert("RGB")
  141. self.im.save(outpath,format="JPEG")
  142. return
  143. if outpath.endswith("png"):
  144. if self.im.mode != 'RGBA':
  145. self.im = self.im.convert("RGBA")
  146. self.im.save(outpath)
  147. return
  148. self.im.save(outpath)
  149. def resize(self, width):
  150. re_x = int(width)
  151. re_y = int(self.y * re_x / self.x)
  152. self.im = self.im.resize((re_x, re_y), Image.Resampling.LANCZOS)
  153. self.x, self.y = self.im.size
  154. def resize_regular(self, width, high):
  155. self.im = self.im.resize((width, high), Image.Resampling.LANCZOS)