-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodify.py
More file actions
77 lines (64 loc) · 2.34 KB
/
modify.py
File metadata and controls
77 lines (64 loc) · 2.34 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
70
71
72
73
74
75
76
77
from PIL import Image, ImageOps
class Bitmap(object):
def __init__(self, file_path):
"""
Records original file path.
"""
self.file_path = file_path
self.img = Image.open(self.file_path)
def new_file_path(self, mod_type):
"""
Create a new file path with the mod type
"""
split_name = self.file_path.split('.')
split_name[1] += f'{mod_type}'
return '.'.join(split_name)
def make_grayscale(self, img='self.img'):
"""
Modify the input image with grayscale and returns the new path with the new image
"""
new_path = self.new_file_path('_grayscale')
img = self.img.convert('L')
return [new_path, img]
def flip_horizontal(self, img='self.img'):
"""
Modify the input image with flip horizontal and returns the new path with the new image
"""
new_path = self.new_file_path('_horizontal')
img = self.img.transpose(Image.FLIP_LEFT_RIGHT)
return [new_path, img]
def flip_vertical(self, img='self.img'):
"""
Modify the input image with flip vertical and returns the new path with the new image
"""
new_path = self.new_file_path('_vertical')
img = self.img.transpose(Image.FLIP_TOP_BOTTOM)
return [new_path, img]
def invert_colors(self, img='self.img'):
"""
Modify the input image with invert colors and returns the new path with the new image
"""
new_path = self.new_file_path('_invert')
img = ImageOps.invert(self.img)
return [new_path, img]
def make_thumbnail(self, img='self.img'):
"""
Modify the input image into a thumbnail and returns the new path with the new image
"""
new_path = self.new_file_path('_thumbnail')
size = 128, 128
img = self.img.copy()
img.thumbnail(size)
return [new_path, img]
def save_new(self, lst):
"""
Save the modified image into the folder
"""
lst[1].save(lst[0], 'bmp')
if __name__ == "__main__":
tiger = Bitmap('./assets/tiger.bmp')
tiger.save_new(tiger.make_grayscale())
tiger.save_new(tiger.flip_horizontal())
tiger.save_new(tiger.flip_vertical())
tiger.save_new(tiger.make_thumbnail())
tiger.save_new(tiger.invert_colors())