FarelDeveloper commited on
Commit
4b945f4
·
verified ·
1 Parent(s): 86cf069

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from fastapi.responses import Response
3
+ from PIL import Image, ImageDraw, ImageFont
4
+ import io
5
+ import textwrap
6
+
7
+ app = FastAPI(
8
+ title="USTADZ API",
9
+ description="API generator gambar ustadz",
10
+ version="1.0"
11
+ )
12
+
13
+ @app.get("/")
14
+ def home():
15
+ return {
16
+ "creator": "Muhammad Farel",
17
+ "api": "USTADZ Generator",
18
+ "endpoint": "/ustadz?text=halo"
19
+ }
20
+
21
+ @app.get("/ustadz")
22
+ def ustadz(text: str = "Contoh Soalan"):
23
+
24
+ img = Image.open("template.jpg").convert("RGB")
25
+ draw = ImageDraw.Draw(img)
26
+
27
+ width, height = img.size
28
+
29
+ try:
30
+ font = ImageFont.truetype("DejaVuSans-Bold.ttf", 50)
31
+ except:
32
+ font = ImageFont.load_default()
33
+
34
+ max_width = 900
35
+
36
+ lines = []
37
+ words = text.split()
38
+
39
+ current_line = ""
40
+
41
+ for word in words:
42
+ test_line = current_line + word + " "
43
+ w = draw.textbbox((0,0), test_line, font=font)[2]
44
+
45
+ if w <= max_width:
46
+ current_line = test_line
47
+ else:
48
+ lines.append(current_line)
49
+ current_line = word + " "
50
+
51
+ lines.append(current_line)
52
+
53
+ y = 200
54
+
55
+ for line in lines:
56
+
57
+ text_width = draw.textbbox((0,0), line, font=font)[2]
58
+ x = (width - text_width) / 2
59
+
60
+ draw.text((x,y), line, fill="black", font=font)
61
+
62
+ y += 60
63
+
64
+ buffer = io.BytesIO()
65
+ img.save(buffer, format="PNG")
66
+
67
+ return Response(buffer.getvalue(), media_type="image/png")