|
| 1 | +import sys |
| 2 | +import os |
| 3 | +from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QFileDialog, QMessageBox |
| 4 | +from PIL import Image |
| 5 | + |
| 6 | +class IconConverterApp(QWidget): |
| 7 | + def __init__(self): |
| 8 | + super().__init__() |
| 9 | + self.setWindowTitle("PNG to Icon Converter") |
| 10 | + self.setGeometry(200, 200, 300, 120) |
| 11 | + |
| 12 | + layout = QVBoxLayout() |
| 13 | + self.button = QPushButton("Select PNG and Convert") |
| 14 | + self.button.clicked.connect(self.convert_icon) |
| 15 | + layout.addWidget(self.button) |
| 16 | + |
| 17 | + self.setLayout(layout) |
| 18 | + |
| 19 | + def convert_icon(self): |
| 20 | + # Step 1: Select PNG file |
| 21 | + png_file, _ = QFileDialog.getOpenFileName(self, "Select PNG File", "", "PNG Files (*.png)") |
| 22 | + if not png_file: |
| 23 | + return |
| 24 | + |
| 25 | + # Step 2: Ask where to save icons |
| 26 | + save_dir = QFileDialog.getExistingDirectory(self, "Select Save Directory") |
| 27 | + if not save_dir: |
| 28 | + return |
| 29 | + |
| 30 | + try: |
| 31 | + img = Image.open(png_file).convert("RGBA") |
| 32 | + |
| 33 | + # Windows ICO |
| 34 | + ico_path = os.path.join(save_dir, "icon.ico") |
| 35 | + img.save(ico_path, format="ICO", sizes=[(16,16), (32,32), (48,48), (256,256)]) |
| 36 | + |
| 37 | + # macOS ICNS |
| 38 | + icns_path = os.path.join(save_dir, "icon.icns") |
| 39 | + img.save(icns_path, format="ICNS") |
| 40 | + |
| 41 | + # Linux PNG sizes |
| 42 | + linux_sizes = [16, 24, 32, 48, 64, 128, 256, 512] |
| 43 | + linux_dir = os.path.join(save_dir, "linux_icons") |
| 44 | + os.makedirs(linux_dir, exist_ok=True) |
| 45 | + for size in linux_sizes: |
| 46 | + resized = img.resize((size, size), Image.LANCZOS) |
| 47 | + resized.save(os.path.join(linux_dir, f"icon_{size}x{size}.png")) |
| 48 | + |
| 49 | + QMessageBox.information(self, "Success", f"Icons saved in:\n{save_dir}") |
| 50 | + |
| 51 | + except Exception as e: |
| 52 | + QMessageBox.critical(self, "Error", str(e)) |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + app = QApplication(sys.argv) |
| 56 | + window = IconConverterApp() |
| 57 | + window.show() |
| 58 | + sys.exit(app.exec()) |
0 commit comments