|
| 1 | +import tkinter as tk |
| 2 | +from tkinter import filedialog, messagebox |
| 3 | +from PIL import Image |
| 4 | +root = tk.Tk() # Tkinter window initialized |
| 5 | +root.title('Converter') # Title of the window |
| 6 | +canvas1 = tk.Canvas(root, width=300, height=250, bg='orange', relief='raised') |
| 7 | +canvas1.pack() |
| 8 | +label1 = tk.Label(root, text='File Converter', bg='lightsteelblue2') # giving a title to the screen |
| 9 | +label1.config(font=('helvetica', 20)) |
| 10 | +canvas1.create_window(150, 60, window=label1) |
| 11 | +im1 = None # variable to store path of image |
| 12 | + |
| 13 | + |
| 14 | +def getJPG(): |
| 15 | + '''Function to get image location and open it with pillow''' |
| 16 | + global im1 |
| 17 | + import_file_path = filedialog.askopenfilename() |
| 18 | + im1 = Image.open(import_file_path) |
| 19 | + |
| 20 | + |
| 21 | +font = ('helvetica', 12, 'bold') |
| 22 | +bg = 'royalblue' |
| 23 | +fg = 'white' |
| 24 | +browseButton_JPG = tk.Button(text=" Import JPEG File ", command=getJPG, bg=bg, fg=fg, font=font) # Browse button |
| 25 | +canvas1.create_window(150, 130, window=browseButton_JPG) |
| 26 | + |
| 27 | + |
| 28 | +def convertToPNG(): |
| 29 | + '''Function to change file extenstion to png and save it to User's prefered location ''' |
| 30 | + global im1 |
| 31 | + if im1 is None: |
| 32 | + tk.messagebox.showerror("Error", "No File selected") |
| 33 | + else: |
| 34 | + export_file_path = filedialog.asksaveasfilename(defaultextension='.png') |
| 35 | + im1.save(export_file_path) |
| 36 | + |
| 37 | + |
| 38 | +saveAsButton_PNG = tk.Button(text='Convert JPEG to PNG', command=convertToPNG, bg=bg, fg=fg, font=font) # Convert button |
| 39 | +canvas1.create_window(150, 180, window=saveAsButton_PNG) |
| 40 | +root.mainloop() |
0 commit comments