1
+ import tkinter as tk
2
+ from tkinter import filedialog , messagebox
3
+ from PIL import Image
4
+
5
+ # Initialize Tkinter window
6
+ root = tk .Tk ()
7
+ # Initialize variable to store image path
8
+ img = None
9
+ # Initialize font, background color, foreground color and width for the buttons
10
+ font = ('helvetica' , 12 , 'bold' )
11
+ bg = 'blue'
12
+ fg = 'white'
13
+ width = 15
14
+
15
+ def getPNG ():
16
+ '''Function to get png image location and open it with pillow'''
17
+ global img
18
+ import_file_path = filedialog .askopenfilename (filetypes = [("PNG File" ,'.png' )])
19
+ img = Image .open (import_file_path )
20
+
21
+ def convertToICO ():
22
+ global img
23
+ '''Function to convert image from png to ico format with pillow and save to user specified location'''
24
+ if img is None :
25
+ tk .messagebox .showerror ("Error" , "No File selected" )
26
+ else :
27
+ export_file_path = filedialog .asksaveasfilename (defaultextension = '.ico' )
28
+ img .save (export_file_path )
29
+ tk .messagebox .showinfo ("Success" , "File converted and saved" )
30
+
31
+ # Set the window title
32
+ root .title ('PNG to ICO Converter' )
33
+ canvas1 = tk .Canvas (root , width = 500 , height = 350 , bg = 'lightblue' )
34
+ canvas1 .pack ()
35
+ # Set the screen title
36
+ label1 = tk .Label (root , text = 'PNG to ICO Converter' , bg = 'lightblue' )
37
+ label1 .config (font = ('helvetica' , 20 ))
38
+ canvas1 .create_window (250 , 100 , window = label1 )
39
+ # Browse button to browse for image
40
+ browseButton = tk .Button (text = "Import PNG File" , command = getPNG , bg = bg , fg = fg , font = font , width = width )
41
+ canvas1 .create_window (250 , 150 , window = browseButton )
42
+ # Convert button to convert selected image and save
43
+ saveAsButton = tk .Button (text = 'Convert PNG to ICO' , command = convertToICO , bg = bg , fg = fg , font = font , width = width )
44
+ canvas1 .create_window (250 , 200 , window = saveAsButton )
45
+ root .mainloop ()
0 commit comments