-
Notifications
You must be signed in to change notification settings - Fork 2
/
multi_image.py
152 lines (137 loc) · 4.89 KB
/
multi_image.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from botocore.client import Config
from PIL import Image
import boto3
import ast
import sys
import uuid
import os
import time
import shutil
import json
import glob
from Queue import Queue
from threading import Thread
s3_client = boto3.client(
's3',
config=Config(signature_version='s3v4')
)
file_size = 0
class Images(object):
def __init__(self, local_path = None, bucket = None, s3_path = None, pri = None):
self.local_path = local_path
self.bucket = bucket
self.s3_path = s3_path
self.pri = pri
max_jobs = 1000
max_workers = 12
queue_s3 = Queue(max_jobs)
class Resolution(object):
def __init__(self, id = None, height = None, width = None, dstPath = None):
self.id = id
self.height = height
self.width = width
self.dstPath = dstPath
def resize_image(image_path, resized_path, height, width):
with Image.open(image_path) as image:
size = (height, width)
#image.thumbnail(size, Image.ANTIALIAS)
temp = image.resize(size, Image.ANTIALIAS)
temp.save(resized_path, 'JPEG', quality = 100)
def process_crops(image_path, crop_size, X, Y):
with Image.open(image_path) as image:
width, height = image.size
for i in range(X):
for j in range(Y):
temp1 = (j+1) * crop_size
if(temp1 > width):
temp1 = width
temp2 = (i+1) * crop_size
if(temp2 > height):
temp2 = height
box = (j * crop_size, i * crop_size, temp1, temp2)
yield image.crop(box)
def crop_image(image_path, crop_path, crop_size, X, Y):
start_num = 0
for k, piece in enumerate(process_crops(image_path, crop_size, X, Y), start_num):
img = Image.new('RGB', (piece.size), 255)
img.paste(piece)
path = os.path.join(crop_path, "X%sY%s.webp" % (k % Y, int(k / Y)))
img.save(path, 'webp', quality = 100)
def s3_uploader(q):
while True:
temp = q.get()
try:
s3_client.upload_file(temp.local_path, temp.bucket, temp.s3_path, ExtraArgs={'ACL': temp.pri, 'ContentType': 'image/jpeg', 'CacheControl': 'max-age=31536000'})
except:
print (temp.s3_path)
q.task_done()
def upload(images_path, dst_s3, bucket, orig, event, pri):
try:
os.remove(images_path+"2.jpg")
except OSError:
print("image 2 not found")
try:
os.remove(images_path+"3.jpg")
except OSError:
print("image 3 not found")
try:
os.remove(images_path+"4.jpg")
except OSError:
print("image 4 not found")
img = Image.open(orig)
try:
os.remove(orig)
except OSError:
print("original image not found")
for root, dirs, files in os.walk(images_path):
for filename in files:
local_path = os.path.join(root, filename)
relative_path = os.path.relpath(local_path, images_path)
s3_path = os.path.join(dst_s3, relative_path)
queue_s3.put(Images(local_path, bucket, s3_path, pri))
for i in range(max_workers):
worker = Thread(target=s3_uploader, args=(queue_s3,))
worker.setDaemon(True)
worker.start()
queue_s3.join()
def handler(event, context):
base_destination_path = "/tmp/" + str(uuid.uuid4()) + "/"
if not os.path.exists(base_destination_path):
os.makedirs(base_destination_path)
bucket = event["bucketName"]
dst_s3 = event["writePath"]
key = event["loadPath"]
pri = event["acl"]
infile = base_destination_path + 'original'+key[key.rfind('.'):]
s3_client.download_file(bucket, key, infile)
resolutions = []
temp = ast.literal_eval(event["res"])
file_size = os.stat(infile).st_size
for i in range(0, len(temp)):
resolutions.append(Resolution(i + 2, temp[i] / 2, temp[i], base_destination_path + "level-"+str(i + 2)))
with Image.open(infile) as im:
width, height = im.size
for i in range(0, len(resolutions)):
save_path = base_destination_path + str(resolutions[i].id)+ '.jpg'
resize_image(infile, save_path, resolutions[i].width, resolutions[i].height)
dst_path = base_destination_path + "level-" + str(resolutions[i].id)
if not os.path.exists(dst_path):
os.makedirs(dst_path)
X = resolutions[i].height / 512
if(resolutions[i].height % 512 > 0):
X = X + 1
Y = resolutions[i].width / 512
if(resolutions[i].width % 512 > 0):
Y = Y + 1
crop_image(save_path, dst_path, 512, X, Y)
upload(base_destination_path, dst_s3, bucket, infile, event, pri)
shutil.rmtree(base_destination_path)
message = {
'message': 'Execution started successfully!',
'file_size': file_size
}
return {
'statusCode': 200,
'headers': {'Content-Type': 'application/json'},
'body': json.dumps(message)
}