欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Python實(shí)現(xiàn)圖片轉(zhuǎn)ICO格式

 更新時間:2025年01月15日 09:21:32   作者:黑客白澤  
這篇文章主要為大家詳細(xì)介紹了如何使用Python編寫一個基于PyQt5的用于將圖像文件轉(zhuǎn)換為ICO格式GUI應(yīng)用程序,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

1. 簡介

這個工具實(shí)現(xiàn)了一個基于PyQt5的用于將圖像文件(如PNG、JPEG、BMP、GIF)轉(zhuǎn)換為ICO格式GUI應(yīng)用程序。以下是該工具的功能介紹:

UI布局:

  • 用于選擇要轉(zhuǎn)換的圖像的文件輸入。
  • 用于指定將生成的ICO文件保存在何處的輸出路徑輸入。
  • 用于選擇所需圖標(biāo)大小(16x16、32x32、48x48、64x64、128x128、256x256)的組合框。 轉(zhuǎn)換前預(yù)覽圖像的區(qū)域。
  • “轉(zhuǎn)換為ICO”按鈕以執(zhí)行轉(zhuǎn)換。

轉(zhuǎn)換過程:

  • 使用Python Pillow庫(PIL)來處理圖像操作。
  • 將所選圖像轉(zhuǎn)換為所需大?。ㄒ訧CO文件的形式)。
  • 如果轉(zhuǎn)換成功,轉(zhuǎn)換將記錄到文件(conversion_history.log)中。

拖放支持:

您可以將圖像文件拖放到應(yīng)用程序中,它將自動加載到輸入字段中并帶有預(yù)覽。

錯誤處理:

對于丟失的文件或轉(zhuǎn)換失敗,會顯示正確的錯誤消息。 如果有其他問題,可以評論區(qū)告訴我!

2. 運(yùn)行效果

3. 相關(guān)源碼

import sys
import os
from PyQt5.QtWidgets import (
    QApplication, QMainWindow, QLabel, QLineEdit, QPushButton, QFileDialog,
    QVBoxLayout, QHBoxLayout, QWidget, QMessageBox, QComboBox
)
from PyQt5.QtGui import QPixmap, QIcon
from PyQt5.QtCore import Qt
from PyQt5.QtCore import QMimeData
from PyQt5.QtGui import QDragEnterEvent, QDropEvent
from PIL import Image

# 日志文件
LOG_FILE = "conversion_history.log"

class ImageToICOConverter(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("圖片轉(zhuǎn)ICO工具")
        self.setGeometry(100, 100, 355, 360)
        self.setAcceptDrops(True)  # 啟用拖拽功能
        self.initUI()

    def initUI(self):
        # 主布局
        main_layout = QVBoxLayout()

        # 圖片選擇
        file_layout = QHBoxLayout()
        self.image_path_input = QLineEdit(self)
        browse_button = QPushButton("瀏覽", self)
        browse_button.clicked.connect(self.choose_image_file)
        file_layout.addWidget(QLabel("選擇圖片文件:"))
        file_layout.addWidget(self.image_path_input)
        file_layout.addWidget(browse_button)
        main_layout.addLayout(file_layout)

        # 輸出路徑選擇
        output_layout = QHBoxLayout()
        self.output_path_input = QLineEdit(self)
        save_button = QPushButton("保存", self)
        save_button.clicked.connect(self.choose_output_path)
        output_layout.addWidget(QLabel("選擇輸出路徑:"))
        output_layout.addWidget(self.output_path_input)
        output_layout.addWidget(save_button)
        main_layout.addLayout(output_layout)

        # 圖標(biāo)尺寸選擇(單選)
        size_layout = QHBoxLayout()
        self.size_combo = QComboBox(self)
        self.size_combo.addItems(["16", "32", "48", "64", "128", "256"])
        size_layout.addWidget(QLabel("選擇圖標(biāo)尺寸:"))
        size_layout.addWidget(self.size_combo)
        main_layout.addLayout(size_layout)

        # 圖片預(yù)覽
        self.preview_label = QLabel("圖片預(yù)覽", self)
        self.preview_label.setAlignment(Qt.AlignCenter)
        self.preview_label.setStyleSheet("background-color: lightgray; border: 1px solid black;")
        self.preview_label.setFixedSize(200, 200)
        preview_layout = QVBoxLayout()
        preview_layout.addWidget(self.preview_label, alignment=Qt.AlignCenter)
        main_layout.addLayout(preview_layout)

        # 轉(zhuǎn)換按鈕
        convert_button = QPushButton("轉(zhuǎn)換為ICO", self)
        convert_button.clicked.connect(self.convert_to_ico)
        main_layout.addWidget(convert_button, alignment=Qt.AlignCenter)

        # 設(shè)置中央窗口
        central_widget = QWidget()
        central_widget.setLayout(main_layout)
        self.setCentralWidget(central_widget)

    def choose_image_file(self):
        file_path, _ = QFileDialog.getOpenFileName(self, "選擇圖片文件", "", "圖片文件 (*.png *.jpg *.jpeg *.bmp *.gif)")
        if file_path:
            self.image_path_input.setText(file_path)
            self.show_preview(file_path)

    def choose_output_path(self):
        output_path, _ = QFileDialog.getSaveFileName(self, "選擇輸出路徑", "", "ICO文件 (*.ico)")
        if output_path:
            self.output_path_input.setText(output_path)

    def show_preview(self, image_path):
        try:
            pixmap = QPixmap(image_path)
            pixmap = pixmap.scaled(200, 200, Qt.KeepAspectRatio, Qt.SmoothTransformation)
            self.preview_label.setPixmap(pixmap)
        except Exception as e:
            QMessageBox.critical(self, "預(yù)覽錯誤", f"無法加載圖片預(yù)覽: {str(e)}")

    def convert_to_ico(self):
        image_path = self.image_path_input.text()
        output_path = self.output_path_input.text()

        if not image_path:
            QMessageBox.critical(self, "錯誤", "請選擇源圖片文件")
            return

        if not output_path:
            output_path = os.path.splitext(image_path)[0] + ".ico"
            self.output_path_input.setText(output_path)

        try:
            img = Image.open(image_path)

            # 獲取用戶選擇的圖標(biāo)尺寸
            size = int(self.size_combo.currentText())
            sizes = [(size, size)]

            img = img.convert("RGBA")
            img.save(output_path, format="ICO", sizes=sizes)

            with open(LOG_FILE, "a") as log_file:
                log_file.write(f"Converted: {image_path} -> {output_path}\n")

            QMessageBox.information(self, "成功", f"圖片已成功轉(zhuǎn)換并保存為 {output_path}")
        except Exception as e:
            QMessageBox.critical(self, "轉(zhuǎn)換錯誤", f"轉(zhuǎn)換過程中發(fā)生錯誤: {str(e)}")

    def dragEnterEvent(self, event: QDragEnterEvent):
        if event.mimeData().hasUrls():
            event.accept()
        else:
            event.ignore()

    def dropEvent(self, event: QDropEvent):
        mime_data: QMimeData = event.mimeData()
        if mime_data.hasUrls():
            file_path = mime_data.urls()[0].toLocalFile()
            self.image_path_input.setText(file_path)
            self.show_preview(file_path)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = ImageToICOConverter()
    window.show()
    sys.exit(app.exec_())

到此這篇關(guān)于使用Python實(shí)現(xiàn)圖片轉(zhuǎn)ICO格式的文章就介紹到這了,更多相關(guān)Python圖片轉(zhuǎn)ICO內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論