-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage-transition.py
116 lines (88 loc) · 3.52 KB
/
image-transition.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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import ctypes
import time
from PIL import Image, ImageEnhance
import os
from multiprocessing import Pool
import atexit
class ImageTransition:
def __init__(self, input_image, output_image, temporary_dir, debug=True, quality=40, num_of_images=15, direct_wp=False):
self.image1 = input_image
self.image2 = output_image
self.temp_dir = temporary_dir
self.debug = debug
self.quality = quality
self.num_of_images = num_of_images
self.direct_wp = direct_wp
self.__check_image_loadable()
self.clean_dir()
self.printDebug(f"Temporary Directory: {temporary_dir}")
self.gen_temporary_imgs()
atexit.register(self.clean_dir)
def __check_image_loadable(self):
try:
Image.open(self.image1)
Image.open(self.image2)
except:
raise IOError("Error loading file")
def setwp(self, img):#works only for windows
ctypes.windll.user32.SystemParametersInfoW(20, 0, img, 0)
#changes brightness for a PIL Image
def change_brightness(self, img:Image, value):
enhancer = ImageEnhance.Brightness(img)
enhanced_im = enhancer.enhance(value)
return enhanced_im
def clean_dir(self):
self.printDebug("> Deleting temp files...")
for filename in os.listdir(self.temp_dir):
if filename.split(".")[1] in ["png","jpg"]:
try:
os.remove(os.path.join(self.temp_dir, filename))
except:
time.sleep(2)
os.remove(os.path.join(self.temp_dir, filename))
def transition_brightness(self, fps=25):
print("> Transition")
for filename in sorted(os.listdir(self.temp_dir), key=lambda x: int(os.path.splitext(x)[0])):
file_type = filename[-3:]
if file_type in ["png", "jpg"]:
#print(filename)
time.sleep(1 / fps)
if self.direct_wp:
self.setwp(os.path.join(self.temp_dir, filename))
else:
yield os.path.join(self.temp_dir, filename)
if self.direct_wp:
self.setwp(self.image2)
else:
yield self.image2
time.sleep(5)
self.clean_dir()
def printDebug(self, msg):
if self.debug:
print(msg)
def worker(self, work_data):
counter = abs(work_data[1])
if work_data[1] <= 0:
value = round(0 - (work_data[1] * (1 / self.num_of_images)), 2)
counter += (self.num_of_images)
else:
value = round(1 - (work_data[1] * (1 / self.num_of_images)), 2)
temp_img = Image.open(work_data[0])
# temp_img.thumbnail([1280, 720], Image.ANTIALIAS) # resize for better performace
temp_img.thumbnail([1920, 1080], Image.ANTIALIAS)
temp_img = self.change_brightness(temp_img, value=value)
#error without this
if temp_img.mode in ("RGBA", "P"):
temp_img = temp_img.convert("RGB")
temp_img.save(self.temp_dir+"/{:02d}.jpg".format(counter), optimize=True, quality=self.quality)
def load(self):
values = []
for i in range(1, self.num_of_images + 1):
values.append((self.image1, i))
for i in range(1, self.num_of_images + 1):
values.append((self.image2, -i))
return values
def gen_temporary_imgs(self):
p = Pool(processes=2)
p.map(self.worker, self.load())
self.printDebug("> Generated temporary images")