-
Notifications
You must be signed in to change notification settings - Fork 0
/
File_Handler.py
46 lines (43 loc) · 1.5 KB
/
File_Handler.py
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
from tkinter import filedialog
from PIL import Image
def save_image_file(image, path = None):
"""
Saves the given image to the given path
Opens a file dialog if no path is given
"""
if path is None:
path = filedialog.asksaveasfilename(defaultextension=".png")
if image is None or type(image) is not Image.Image:
raise ValueError('Invalid image')
image.save(path)
def upload_image_file(filename = None):
"""
Opens the given file and returns the image
Opens a file dialog if no filename is given
"""
if filename is None:
f_types = [('PNG Files', '*.png')]
filename = filedialog.askopenfilename(filetypes=f_types)
return Image.open(filename)
def save_file(data, path = None, extension = None):
"""
Saves the given data to the given path
Opens a file dialog if no path is given
"""
if path is None:
if extension is not None:
path = filedialog.asksaveasfilename(defaultextension=extension)
else:
path = filedialog.asksaveasfilename()
with open(path, 'wb') as file:
file.write(data)
def upload_file(filename = None):
"""
Opens the given file and returns the data
Opens a file dialog if no filename is given
"""
if filename is None:
filename = filedialog.askopenfilename()
filename_trimmed = filename.split('/')[-1]
with open(filename, 'rb') as file:
return file.read(), filename_trimmed