-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
64 lines (50 loc) · 1.62 KB
/
server.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
from concurrent import futures
import logging
import grpc
import service_shotifypy_pb2_grpc
import rpc_make_img_pb2
import imgkit
from PIL import Image
def gen_img_from_html(html):
FILE_NAME = "source.html"
OUTPUT_FILE = 'out.png'
with open(FILE_NAME, "w") as file:
file.write(html)
file.close()
options = {
'encoding': "UTF-8",
}
imgkit.from_file(FILE_NAME, 'out.png', options=options)
print("Starting to generate image from html")
image = Image.open(OUTPUT_FILE)
# Get the current size of the image
width, height = image.size
# Set the desired width
desired_width = 600
# Calculate the left and right coordinates for cropping
left = (width - desired_width) // 2
right = left + desired_width
# Define the area to crop
area = (left, 0, right, height)
# Crop the image
cropped_image = image.crop(area)
cropped_image.save(OUTPUT_FILE)
class Shotifypy(service_shotifypy_pb2_grpc.ShotifypyServicer):
def HtmlSnapshot(self, request, context):
print("Start")
# request.html
print(request.html)
gen_img_from_html(request.html)
return rpc_make_img_pb2.MakeImageFromHtmlResponse(message="Done")
def serve():
port = '6060'
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
service_shotifypy_pb2_grpc.add_ShotifypyServicer_to_server(
Shotifypy(), server)
server.add_insecure_port('[::]:' + port)
server.start()
print("Server started, listening on " + port)
server.wait_for_termination()
if __name__ == '__main__':
logging.basicConfig()
serve()