Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image, ImageDraw, ImageFont
|
3 |
+
import cv2
|
4 |
+
import numpy as np
|
5 |
+
import time
|
6 |
+
import random
|
7 |
+
import os
|
8 |
+
import requests
|
9 |
+
|
10 |
+
|
11 |
+
# Muat gambar dasar (gambar.png)
|
12 |
+
gambar_dasar = cv2.imread("gambar.png")
|
13 |
+
def tambahkan_teks_di_tengah_dengan_offset_y(teks, email):
|
14 |
+
|
15 |
+
# Konversi gambar OpenCV (numpy array) ke PIL image
|
16 |
+
print(email)
|
17 |
+
url = "https://daurah.al-wafi.sch.id/api/cert/" + email
|
18 |
+
response = requests.get(url)
|
19 |
+
data = response.json()
|
20 |
+
if data.get("message") == "not eligible":
|
21 |
+
return "Error"
|
22 |
+
|
23 |
+
gambar = cv2.imread("gambar.png")
|
24 |
+
offset_y = -240
|
25 |
+
gambar_pil = Image.fromarray(cv2.cvtColor(gambar, cv2.COLOR_BGR2RGB))
|
26 |
+
draw = ImageDraw.Draw(gambar_pil)
|
27 |
+
|
28 |
+
# Gunakan font yang mendukung karakter Arab
|
29 |
+
font_path = "Arial.ttf" # Ganti dengan path ke file font .ttf Anda
|
30 |
+
font_size = 68 # Ukuran font
|
31 |
+
font = ImageFont.truetype(font_path, font_size)
|
32 |
+
|
33 |
+
# Ukur ukuran teks
|
34 |
+
lebar_teks, tinggi_teks = draw.textsize(teks, font=font)
|
35 |
+
|
36 |
+
# Hitung posisi teks
|
37 |
+
tinggi, lebar = gambar.shape[:2]
|
38 |
+
posisi_x = (lebar - lebar_teks) // 2
|
39 |
+
posisi_y = (tinggi - tinggi_teks) // 2 + offset_y
|
40 |
+
|
41 |
+
# Tambahkan teks ke gambar
|
42 |
+
draw.text((posisi_x, posisi_y), teks, font=font, fill="black")
|
43 |
+
|
44 |
+
# Konversi kembali ke gambar OpenCV (numpy array)
|
45 |
+
gambar = cv2.cvtColor(np.array(gambar_pil), cv2.COLOR_RGB2BGR)
|
46 |
+
#Buat nama file hasil dengan nomor acak
|
47 |
+
current_time = int(time.time())
|
48 |
+
random_numbers = ''.join(random.choices('0123456789', k=5))
|
49 |
+
hasil_filename = f'{teks}_{random_numbers}.jpeg'
|
50 |
+
hasil_path = os.path.join('static', 'cert2', hasil_filename)
|
51 |
+
height, width = gambar.shape[:2]
|
52 |
+
center_x = width // 2
|
53 |
+
center_y = height // 2
|
54 |
+
new_width = width // 2
|
55 |
+
new_height = height // 2
|
56 |
+
# Mengubah ukuran gambar
|
57 |
+
gambar = cv2.resize(gambar, (new_width, new_height))
|
58 |
+
cv2.imwrite(hasil_path, gambar)
|
59 |
+
return cv2.cvtColor(np.array(gambar), cv2.COLOR_RGB2BGR)
|
60 |
+
# Fungsi untuk menambahkan teks ke gambar
|
61 |
+
def tambahkan_teks(text, offset_y, font_size=68):
|
62 |
+
img = cv2.imread("gambar.png")
|
63 |
+
offset_y = -240
|
64 |
+
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
|
65 |
+
draw = ImageDraw.Draw(img)
|
66 |
+
|
67 |
+
# Gunakan font (pastikan Anda memiliki Arial.ttf atau font yang mendukung karakter Arab)
|
68 |
+
font_path = "Arial.ttf"
|
69 |
+
font = ImageFont.truetype(font_path, font_size)
|
70 |
+
|
71 |
+
# Hitung posisi teks
|
72 |
+
lebar_teks, tinggi_teks = draw.textsize(text, font=font)
|
73 |
+
tinggi, lebar = img.size
|
74 |
+
posisi_x = (lebar - lebar_teks) // 2
|
75 |
+
posisi_y = (tinggi - tinggi_teks) // 2 + offset_y
|
76 |
+
|
77 |
+
# Tambahkan teks
|
78 |
+
draw.text((posisi_x, posisi_y), text, font=font, fill="black")
|
79 |
+
|
80 |
+
# Simpan gambar dengan nama file unik
|
81 |
+
current_time = int(time.time())
|
82 |
+
random_numbers = ''.join(random.choices('0123456789', k=5))
|
83 |
+
hasil_filename = f'{text}_{random_numbers}.jpeg'
|
84 |
+
hasil_path = os.path.join('cert2', hasil_filename)
|
85 |
+
img.save(hasil_path)
|
86 |
+
|
87 |
+
return cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
|
88 |
+
|
89 |
+
# Gradio interface
|
90 |
+
with gr.Blocks() as demo:
|
91 |
+
gr.Markdown("## Tambahkan Nama ke Sertifikat (Hanya sekali download)")
|
92 |
+
|
93 |
+
with gr.Row():
|
94 |
+
text_input = gr.Textbox(label="Nama (Arab)") # Input teks tanpa nilai awal
|
95 |
+
email_input = gr.Textbox(label="Email")
|
96 |
+
offset_y_input = gr.Slider(-1500, 300, value=-240, step=10, label="Offset Vertikal (Y)")
|
97 |
+
font_size_input = gr.Slider(30, 100, value=68, step=5, label="Ukuran Font")
|
98 |
+
|
99 |
+
image_output = gr.Image(label="Hasil")
|
100 |
+
|
101 |
+
btn_process = gr.Button("Proses Sertifikat")
|
102 |
+
btn_process.click(
|
103 |
+
tambahkan_teks_di_tengah_dengan_offset_y,
|
104 |
+
inputs=[text_input, email_input],
|
105 |
+
outputs=image_output,
|
106 |
+
)
|
107 |
+
|
108 |
+
demo.launch(debug=True)
|