-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproject-3.py
More file actions
69 lines (45 loc) · 1.96 KB
/
project-3.py
File metadata and controls
69 lines (45 loc) · 1.96 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from tkinter import Tk, Button, Label, Frame
from tkinter.filedialog import askopenfilename
from PIL import Image, ImageTk
def convert_to_jpg():
global image_label, filename_label, saved_label
if image_path:
image = Image.open(image_path)
if image.mode == 'RGBA':
image = image.convert('RGB')
jpg_filename = image_path.replace('.png', '.jpg')
image.save(jpg_filename, 'JPEG')
saved_label.config(text=f"Saved as: {jpg_filename}")
print(f"Image converted and saved as {jpg_filename}.")
def open_image():
global image_path, image_label, filename_label
image_path = askopenfilename(filetypes=[("Png Files", ".png")])
if image_path:
image = Image.open(image_path)
size = min(image.size)
image = image.crop((0, 0, size, size))
image = image.resize((400, 400))
photo = ImageTk.PhotoImage(image)
image_label.configure(image=photo)
image_label.image = photo
filename_label.config(text=f"Selected file: {image_path}")
window = Tk()
window.title("PNG to JPG Converter")
window.geometry("600x600")
window.resizable(False, False)
image_frame = Frame(window, width=250, height=200, relief="solid")
image_frame.place(x=50, y=20)
image_label = Label(image_frame)
image_label.pack(padx=50, pady=20)
filename_label = Label(window, text="Selected file: None", font=("Arial", 12))
filename_label.place(x=0, y=470)
open_button = Button(window, text="Open Image", command=open_image, font=("Arial", 14))
open_button.place(x=0, y=500)
convert_button = Button(window, text="Convert to JPG", command=convert_to_jpg, font=("Arial", 14))
convert_button.place(x=150, y=500)
saved_label = Label(window, font=("Arial", 12))
saved_label.place(x=0, y=550)
previewtext_label = Label(window, text="Preview", font=("Arial", 18))
previewtext_label.place(x=250, y=10)
image_path = None
window.mainloop()