File size: 3,244 Bytes
846cddc
 
 
 
 
 
 
 
 
1ca8456
 
 
 
 
 
846cddc
 
 
1ca8456
 
 
 
 
 
846cddc
1ca8456
846cddc
 
 
 
 
 
 
 
1ca8456
 
846cddc
1ca8456
846cddc
 
 
 
 
1ca8456
846cddc
 
 
 
 
1ca8456
846cddc
 
 
 
 
 
 
 
 
 
1ca8456
846cddc
1ca8456
846cddc
 
 
1ca8456
846cddc
 
 
 
 
 
1ca8456
 
 
846cddc
 
 
 
1ca8456
846cddc
 
 
 
1ca8456
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
import os
from PIL import Image, ImageDraw, ImageFont
import gradio as gr
from transformers import pipeline

# 1) 이미지 캑셔닝 νŒŒμ΄ν”„λΌμΈ
captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")

# 2) 밈 + μžλ§‰ ν•©μ„± ν•¨μˆ˜
def make_meme_with_subtitle(image, top_text, bottom_text):
    if image is None:
        raise ValueError("이미지가 μ—…λ‘œλ“œλ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.")
    
    # 이미지 λ³€ν™˜
    img = image.convert("RGB")
    w, h = img.size

    # β‘  μžλ™ μΊ‘μ…˜ 생성
    try:
        out = captioner(img, max_length=40, num_beams=5)
        raw = out[0].get("generated_text") or out[0].get("text") or ""
        subtitle = raw.strip().capitalize() if raw else "No caption found"
    except Exception as e:
        subtitle = "No caption (error)"

    # β‘‘ μžλ§‰μš© 검은 띠 생성
    bar_h = int(h * 0.15)
    bar = Image.new("RGB", (w, bar_h), color="black")
    combined_h = h + bar_h
    combined = Image.new("RGB", (w, combined_h))
    combined.paste(img, (0, 0))
    combined.paste(bar, (0, h))

    draw = ImageDraw.Draw(combined)

    # β‘’ μžλ§‰ 폰트 μ„€μ •
    try:
        font_path = "arialbd.ttf"  # μ‹œμŠ€ν…œμ— ν•΄λ‹Ή ν°νŠΈκ°€ 없을 경우 였λ₯˜
        base_font_size = int(bar_h * 0.5)
        font = ImageFont.truetype(font_path, size=base_font_size)
    except:
        font = ImageFont.load_default()

    # β‘£ μžλ§‰ 그리기 (ν•˜λ‹¨ 쀑앙)
    tw, th = draw.textsize(subtitle, font=font)
    tx = (w - tw) // 2
    ty = h + (bar_h - th) // 2
    draw.text((tx, ty), subtitle, font=font, fill="white")

    # β‘€ 밈 ν…μŠ€νŠΈ 폰트 μ„€μ •
    meme_font_size = int(h * 0.07)
    try:
        meme_font = ImageFont.truetype(font_path, size=meme_font_size)
    except:
        meme_font = ImageFont.load_default()

    # 상단 ν…μŠ€νŠΈ
    if top_text:
        text = top_text.upper()
        tw, th = draw.textsize(text, font=meme_font)
        draw.text(((w - tw) // 2, 10), text, font=meme_font, fill="white", stroke_width=2, stroke_fill="black")

    # ν•˜λ‹¨ ν…μŠ€νŠΈ (이미지 λ‚΄λΆ€)
    if bottom_text:
        text = bottom_text.upper()
        tw, th = draw.textsize(text, font=meme_font)
        draw.text(((w - tw) // 2, h - th - 10), text, font=meme_font, fill="white", stroke_width=2, stroke_fill="black")

    return combined

# 3) Gradio UI
with gr.Blocks() as demo:
    gr.Markdown("## πŸ“Έ 밈 생성 + μžλ™ μ˜μ–΄ μžλ§‰ ν•©μ„±")
    gr.Markdown("이미지λ₯Ό μ—…λ‘œλ“œν•˜κ³ , μƒλ‹¨Β·ν•˜λ‹¨ 밈 ν…μŠ€νŠΈλ₯Ό μž…λ ₯ν•˜λ©΄ μžλ™ μƒμ„±λœ μ˜μ–΄ μžλ§‰κ³Ό ν•¨κ»˜ 밈 μŠ€νƒ€μΌ 이미지λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.")

    img_in = gr.Image(type="pil", label="Upload Image")  # filepath β†’ pil
    top_txt = gr.Textbox(label="Top Text (optional)", placeholder="e.g. WHEN YOU REALIZE...", lines=1)
    bottom_txt = gr.Textbox(label="Bottom Text (optional)", placeholder="e.g. ...IT'S MONDAY AGAIN", lines=1)
    btn = gr.Button("Generate Meme")
    out_img = gr.Image(label="Meme with Subtitle")
    
    btn.click(fn=make_meme_with_subtitle, inputs=[img_in, top_txt, bottom_txt], outputs=out_img)

# 4) μ•± μ‹€ν–‰
if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))