-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_compressor.py
More file actions
40 lines (34 loc) · 1.36 KB
/
image_compressor.py
File metadata and controls
40 lines (34 loc) · 1.36 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
from PIL import Image
import tkinter as tk
from tkinter import filedialog, messagebox, simpledialog
import os, sys
root = tk.Tk()
root.withdraw()
def open_image(dialog_title):
file_path = filedialog.askopenfilename(title=dialog_title, filetypes=[('Image file', ('.jpg', '.png', '.jpeg'))])
if file_path.strip() == '':
sys.exit()
img_file = open(file_path, 'rb')
return img_file
def open_multiple_images(dialog_title):
files_path = filedialog.askopenfilenames(title=dialog_title, filetypes=[('Image file', ('.jpg', '.png', '.jpeg'))])
img_files = []
for files in files_path:
img_files.append(files)
img_files.sort()
return img_files
def save_this_file(dialog_title, dialog_ext):
file_output = filedialog.asksaveasfile(mode='wb', defaultextension=dialog_ext, title=dialog_title)
if file_output is None:
messagebox.showinfo('Error', 'Location Undefined!')
sys.exit()
return file_output
def compress_img():
img_file = open_image('Choose an image for processing..')
# img_files = open_multiple_images('Choose one or more images for processing..')
image = Image.open(img_file)
img_output = save_this_file('Choose the location to Save output Image', '.png')
image.save(img_output,quality=10)
img_output.close()
messagebox.showinfo('Success', 'Image Compression Successful!')
compress_img()