您好,登錄后才能下訂單哦!
怎么在python中使用pyqt5實現一個圖片批量縮放工具?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
Python是一種跨平臺的、具有解釋性、編譯性、互動性和面向對象的腳本語言,其最初的設計是用于編寫自動化腳本,隨著版本的不斷更新和新功能的添加,常用于用于開發獨立的項目和大型項目。
通過命令行安裝。
pip install pillow pip install pyqt5
代碼:
#!-*-coding:utf-8-*- from PIL import Image import hashlib, os, sys from PyQt5.QtGui import QFont, QIcon from PyQt5.QtWidgets import (QFileDialog, QMessageBox, QGridLayout, QLineEdit, QTextEdit, QLabel, QWidget, QToolTip, QPushButton, QApplication, QInputDialog) from PyQt5.QtCore import QCoreApplication def makedir(path): if not os.path.exists(path): os.mkdir(path) def image_resize(image_path, scale): #獲得新文件名稱 out_path = os.path.splitext(image_path)[0] + "_after" + os.path.splitext(image_path)[-1] with Image.open( image_path ) as im: width = int(im.size[0]*scale) height = int(im.size[1]*scale) #resize 設置新尺寸 out = im.resize( (width, height) ) out.save(out_path) def folder_resize(folder_path, scale): dst_files = [] files = os.listdir( folder_path ) for file in files: ext = os.path.splitext(file)[-1] #只支持jpg或png文件 if ext == ".jpg" or ext == ".png": dst_files.append(file) #輸出路徑 out_folder = os.path.join(folder_path, "after") makedir(out_folder) for item in dst_files: try: op = os.path.join( out_folder, item ) with Image.open( os.path.join(folder_path, item) ) as im: width = int(im.size[0]*scale) height = int(im.size[1]*scale) out = im.resize( (width, height) ) out.save(op) except FileNotFoundError: print("file not found " + item) class MainBody(QWidget): def __init__(self): super(MainBody, self).__init__() self.init() self.m_scale = 1.0 self.m_width = 0 self.m_height = 0 def init(self): grid = QGridLayout() self.setLayout(grid) grid.setSpacing(10) self.m_tedit = QTextEdit() self.m_tedit.setToolTip( u'可以拖拽文件到這里來' ) grid.addWidget(self.m_tedit, 1, 0, 4, 3) self.m_scale_set_btn = QPushButton( u"設置縮放值" ) self.m_scale_set_btn.clicked.connect( self.set_scale_func ) grid.addWidget( self.m_scale_set_btn, 1, 3 ) self.m_scale_label = QLabel( "1.0" ) grid.addWidget( self.m_scale_label, 2, 3 ) self.m_height_set_btn = QPushButton( u"設置高度" ) self.m_height_set_btn.clicked.connect( self.set_height_func ) grid.addWidget( self.m_height_set_btn, 3, 3 ) self.m_height_label = QLabel( "0" ) grid.addWidget( self.m_height_label, 4, 3 ) self.m_width_set_btn = QPushButton( u"設置寬度" ) self.m_width_set_btn.clicked.connect( self.set_width_func ) grid.addWidget( self.m_width_set_btn, 5, 3 ) self.m_width_label = QLabel( "0" ) grid.addWidget( self.m_width_label, 6, 3 ) self.m_file_btn = QPushButton(u'選擇文件') self.m_file_btn.clicked.connect( self.file_func ) self.m_folder_btn = QPushButton(u'選擇文件夾') self.m_folder_btn.clicked.connect( self.folder_func ) self.m_generate_btn = QPushButton(u'處理') self.m_generate_btn.clicked.connect( self.generate_func ) self.m_clear_btn = QPushButton(u'清空') self.m_clear_btn.clicked.connect( self.clear_func ) grid.addWidget(self.m_file_btn, 5, 0) grid.addWidget(self.m_folder_btn, 5, 1) grid.addWidget(self.m_generate_btn, 6, 0) grid.addWidget(self.m_clear_btn, 6, 1) self.setGeometry(300,300,300,200) self.setWindowTitle(u"批量圖片縮放") self.setWindowIcon(QIcon('icon.png')) self.show() #按照輸入的縮放值進行縮放 def set_scale_func(self): text, ok = QInputDialog.getText( self, 'Input Scale', u'輸入縮放值' ) if ok: try: scale = float( text ) except ValueError: QMessageBox.warning( self, u"設置縮放值", u"輸入錯誤" ) else: self.m_scale = scale self.refresh_setting(0) #根據高度的縮放比例等比例縮放 def set_height_func(self): text, ok = QInputDialog.getText( self, 'Input Height', u'輸入高度' ) if ok: try: height = int( text ) except ValueError: QMessageBox.warning( self, u"設置高度", u"輸入錯誤" ) else: self.m_height = height self.refresh_setting(2) #根據寬度的縮放比例等比例縮放 def set_width_func(self): text, ok = QInputDialog.getText( self, 'Input Width', u'輸入寬度' ) if ok: try: width = int( text ) except ValueError: QMessageBox.warning( self, u"設置寬度", u"輸入錯誤" ) else: self.m_width = width self.refresh_setting(1) def file_func(self): filename, ok = QFileDialog.getOpenFileName(self, "Open File", "C:/Users/Administrator/Desktop", "JPG (*.jpg);;PNG (*.png)") if ok: self.m_tedit.setText( filename ) size = self.get_image_size( filename ) s,w,h = self.get_current_setting() self.m_width = int(size[0]*s) self.m_height = int(size[1]*s) self.m_height_label.setText( str( self.m_height ) ) self.m_width_label.setText( str( self.m_width ) ) def folder_func(self): filename = QFileDialog.getExistingDirectory(self, "Open File", "C:/Users/Administrator/Desktop") self.m_tedit.setText( filename ) def generate_func(self): path = self.get_image_path() if path == None or path == "": QMessageBox.warning( self, u"Error", u"未選擇文件" ) return elif os.path.exists(path) == False: QMessageBox.warning( self, u"Error", u"文件不存在" ) return if os.path.isdir(path): folder_resize( path, self.m_scale ) elif os.path.isfile(path): image_resize( path, self.m_scale ) def clear_func(self): self.m_tedit.clear() self.m_width = 0 self.m_height = 0 self.m_width_label.setText("0") self.m_height_label.setText("0") def get_image_size(self, image_path): with Image.open(image_path) as im: return im.size def get_image_path(self): path = self.m_tedit.toPlainText() if path.startswith( 'file:///' ): path = path[8:] return path def get_current_setting(self): return self.m_scale, self.m_width, self.m_height def refresh_setting(self, kind): s, w, h = self.get_current_setting() p = self.get_image_path() if kind == 0: if p == None or p == "" or os.path.exists(p) == False or os.path.isdir(p) == True: w = 0 h = 0 else: size = self.get_image_size(p) w = size[0]*s h = size[1]*s elif kind == 1: if p == None or p == "" or os.path.exists(p) == False: QMessageBox.warning( self, u"Warning", u"請先選擇文件" ) return elif os.path.isdir(p) == True: QMessageBox.warning( self, u"Warning", u"文件夾不能設置寬度" ) return size = self.get_image_size(p) w = int(w) s = w*1.0/size[0] h = int((w/size[0])*size[1]) elif kind == 2: if p == None or p == "" or os.path.exists(p) == False: QMessageBox.warning( self, u"Warning", u"請先選擇文件" ) return elif os.path.isdir(p) == True: QMessageBox.warning( self, u"Warning", u"文件夾不能設置高度" ) return size = self.get_image_size(p) h = int(h) s = h*1.0/size[1] w = int((h/size[1])*size[0]) self.m_width = int(w) self.m_height = int(h) self.m_scale = float(s) self.m_width_label.setText(str(self.m_width)) self.m_height_label.setText(str(self.m_height)) self.m_scale_label.setText(str(self.m_scale)) if __name__ == "__main__": app = QApplication(sys.argv) mb = MainBody() sys.exit( app.exec_() )
看完上述內容,你們掌握怎么在python中使用pyqt5實現一個圖片批量縮放工具的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。