-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage compressor python.py
More file actions
64 lines (52 loc) · 2.47 KB
/
image compressor python.py
File metadata and controls
64 lines (52 loc) · 2.47 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
from PIL import ImageTk
from PIL import Image as PilImage
from tkinter import *
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox, filedialog
class ImageCompressor(Tk):
def __init__(self, winTitle, xSize, ySize, *args):
super(ImageCompressor, self).__init__()
if args:
self.configure(bg=args)
self.geometry(f'{xSize}x{ySize}')
self.title(winTitle)
self.resizable(False,False)
self.compressFile = Button(text="Choose Image", command=self.GetImageFile)
self.compressFile.place(x=25, y=15)
self.chooseQuality = Label(self, text="Choose Image Quality", font=("Courier", 10))
self.chooseQuality.place(x=60,y=70)
self.scaleValue = Scale(self, from_=100, to=0)
self.scaleValue.place(x=0,y=70)
self.saveFolder = Button(text="Choose which folder to save to", command=self.SavedFolder)
self.saveFolder.place(x=62.5, y=100)
self.imageNameLabel = Label(text="Enter new file name")
self.imageName = Entry(self, bd=3)
self.imageName.place(x=62.5, y=160)
self.compressImageBtn = Button(text="Compress image", command=self.CompressImage, bd=5)
self.compressImageBtn.place(x=290, y=90)
self.mainloop()
def GetImageFile(self):
self.compressLocation = filedialog.askopenfilename()
if self.compressLocation:
messagebox.showinfo("File", self.compressLocation)
else:
messagebox.showwarning("Error", "NO image selected")
def SavedFolder(self):
self.saveTo = filedialog.askdirectory()
if self.saveTo:
messagebox.showinfo("Save to:",self.saveTo)
else:
messagebox.showinfo("Error", "No Folder selected")
def CompressImage(self):
self.scaleNum = self.scaleValue.get()
try:
self.imageToCompress = PilImage.open(self.compressLocation)
self.getImageExtension = self.compressLocation.rsplit(".", 1)
self.imageExtension = self.getImageExtension[1]
self.imageEntryName = self.imageName.get()
self.imageToCompress.save(f"{self.saveTo}/{self.imageEntryName}.{self.imageExtension}",quality=self.scaleNum)
messagebox.showinfo("Successful", f"Compressed Image saved to {self.saveTo}")
except:
messagebox.showwarning("Error", "Something went wrong")
MyNewGUI = ImageCompressor("Image Compressor", 458,225)