-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage2ico.py
More file actions
48 lines (38 loc) · 1.39 KB
/
image2ico.py
File metadata and controls
48 lines (38 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from pathlib import Path
from PIL import Image
import tkinter as tk
from tkinter import filedialog, messagebox
def choose_files():
root = tk.Tk()
root.withdraw()
input_path = filedialog.askopenfilename(
title="Choose an image file",
initialdir=r"c:\users\owner\pictures",
filetypes=[
("Image files", "*.png *.jpg *.jpeg *.bmp *.gif *.tiff *.webp *.ico"),
("All files", "*.*"),
],
)
if not input_path:
root.destroy()
return None, None
default_output = str(Path(input_path).with_suffix(".ico"))
output_path = filedialog.asksaveasfilename(
title="Save ICO file as",
initialdir=str(Path(input_path).parent),
initialfile=Path(default_output).name,
defaultextension=".ico",
filetypes=[("ICO files", "*.ico")],
)
root.destroy()
return input_path, output_path
def convert_image_to_ico():
input_path, output_path = choose_files()
if not input_path or not output_path:
return
img = Image.open(input_path).convert("RGBA") # Ensure the image has an alpha channel
# Saving with the specified size is common for ICO files
img.save(output_path, format='ICO', sizes=[(256, 256)])
messagebox.showinfo("Conversion complete", f"Saved icon to:\n{output_path}")
# Usage example:
convert_image_to_ico()