cert / app.py
asaderu's picture
Create app.py
edd5871 verified
raw
history blame
3.75 kB
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)")
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)