File size: 3,823 Bytes
edd5871
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36d9998
edd5871
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from PIL import Image, ImageDraw, ImageFont
import cv2
import numpy as np
import time
import random
import os
import requests


# Muat gambar dasar (gambar.png)
gambar_dasar = cv2.imread("gambar.png")
def tambahkan_teks_di_tengah_dengan_offset_y(teks, email):

    # Konversi gambar OpenCV (numpy array) ke PIL image
    print(email)
    url = "https://daurah.al-wafi.sch.id/api/cert/" + email
    response = requests.get(url)
    data = response.json()
    if data.get("message") == "not eligible":
      return "Error"

    gambar = cv2.imread("gambar.png")
    offset_y = -240
    gambar_pil = Image.fromarray(cv2.cvtColor(gambar, cv2.COLOR_BGR2RGB))
    draw = ImageDraw.Draw(gambar_pil)

    # Gunakan font yang mendukung karakter Arab
    font_path = "Arial.ttf"  # Ganti dengan path ke file font .ttf Anda
    font_size = 68  # Ukuran font
    font = ImageFont.truetype(font_path, font_size)

    # Ukur ukuran teks
    lebar_teks, tinggi_teks = draw.textsize(teks, font=font)

    # Hitung posisi teks
    tinggi, lebar = gambar.shape[:2]
    posisi_x = (lebar - lebar_teks) // 2
    posisi_y = (tinggi - tinggi_teks) // 2 + offset_y

    # Tambahkan teks ke gambar
    draw.text((posisi_x, posisi_y), teks, font=font, fill="black")

    # Konversi kembali ke gambar OpenCV (numpy array)
    gambar = cv2.cvtColor(np.array(gambar_pil), cv2.COLOR_RGB2BGR)
    #Buat nama file hasil dengan nomor acak
    current_time = int(time.time())
    random_numbers = ''.join(random.choices('0123456789', k=5))
    hasil_filename = f'{teks}_{random_numbers}.jpeg'
    hasil_path = os.path.join('static', 'cert2', hasil_filename)
    height, width = gambar.shape[:2]
    center_x = width // 2
    center_y = height // 2
    new_width = width // 2
    new_height = height // 2
    # Mengubah ukuran gambar
    gambar = cv2.resize(gambar, (new_width, new_height))
    cv2.imwrite(hasil_path, gambar)
    return cv2.cvtColor(np.array(gambar), cv2.COLOR_RGB2BGR)
# Fungsi untuk menambahkan teks ke gambar
def tambahkan_teks(text, offset_y, font_size=68):
    img = cv2.imread("gambar.png")
    offset_y = -240
    img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    draw = ImageDraw.Draw(img)

    # Gunakan font (pastikan Anda memiliki Arial.ttf atau font yang mendukung karakter Arab)
    font_path = "Arial.ttf"
    font = ImageFont.truetype(font_path, font_size)

    # Hitung posisi teks
    lebar_teks, tinggi_teks = draw.textsize(text, font=font)
    tinggi, lebar = img.size
    posisi_x = (lebar - lebar_teks) // 2
    posisi_y = (tinggi - tinggi_teks) // 2 + offset_y

    # Tambahkan teks
    draw.text((posisi_x, posisi_y), text, font=font, fill="black")

    # Simpan gambar dengan nama file unik
    current_time = int(time.time())
    random_numbers = ''.join(random.choices('0123456789', k=5))
    hasil_filename = f'{text}_{random_numbers}.jpeg'
    hasil_path = os.path.join('cert2', hasil_filename)
    img.save(hasil_path)

    return cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)

# Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("# Tambahkan Nama ke Sertifikat (Hanya sekali download). Tombol download ada di sisi kanan atas sertifikat (Hanya Sekali Download)")

    with gr.Row():
        text_input = gr.Textbox(label="Nama (Arab)")  # Input teks tanpa nilai awal
        email_input = gr.Textbox(label="Email")
        offset_y_input = gr.Slider(-1500, 300, value=-240, step=10, label="Offset Vertikal (Y)")
        font_size_input = gr.Slider(30, 100, value=68, step=5, label="Ukuran Font")

    image_output = gr.Image(label="Hasil")

    btn_process = gr.Button("Proses Sertifikat")
    btn_process.click(
        tambahkan_teks_di_tengah_dengan_offset_y,
        inputs=[text_input, email_input],
        outputs=image_output,
    )

demo.launch(debug=True)