-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_thumbnails_multi.py
60 lines (45 loc) · 1.87 KB
/
server_thumbnails_multi.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
from PIL import Image
from PIL import ImageEnhance
import io
import time
from lithops import Storage
from multiprocessing import Pool
def create_thumbnail(cos_key, sizes):
storage = Storage()
cos_file = storage.get_object(bucket='cos-lithops-thesis-bucket', key=cos_key)
print("Fetching file: ", cos_key)
output_list = []
for size in sizes:
outfile = cos_key.split("/")[-1] + ".thumbnail" + str(size)
try:
im = Image.open(io.BytesIO(cos_file))
im.thumbnail(size)
#enh = ImageEnhance.Contrast(im)
#im = enh.enhance(1.3)
img_byte_arr = io.BytesIO()
im.save(img_byte_arr, "JPEG")
return_tuple = (outfile, img_byte_arr)
output_list.append(return_tuple)
except OSError:
print("cannot create thumbnail for", cos_key)
return output_list
if __name__ == '__main__':
storage = Storage()
cos_keys = storage.list_keys(bucket='cos-lithops-thesis-bucket', prefix='images/')
image_count = int(input("Number of images to be processed (max: " + str(len(cos_keys)) + "): "))
if image_count < len(cos_keys):
cos_keys = cos_keys[:image_count]
input_files = []
print("Creating %s thumbnails..." % image_count)
start_time = time.time()
sizes = [[(16, 16), (32, 32), (64, 64), (128, 128), (256, 256), (512, 512)]]
with Pool() as pool:
output_files = pool.starmap(create_thumbnail, zip(cos_keys, sizes*len(cos_keys)))
print(output_files)
print("Time needed (VM-Multiprocessing): " + str(time.time() - start_time))
print("Saving thumbnails to bucket")
for images in output_files:
for image in images:
save_key = "thumbnails/" + str(image[0]) + ".jpg"
storage.put_object(bucket='cos-lithops-thesis-bucket', key=save_key, body=image[1].getvalue())
print("Finished")