module_resize_pic.py 6.6 KB

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