File size: 2,194 Bytes
c709870
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import io, os, random
from pathlib import Path
from PIL import Image, ImageDraw, ImageFont, ImageFilter
from image_backends.backend_common import MAX_RETRIES, normalize_image_size, resolve_output_path, save_image_bytes
VALID_ASPECT_RATIOS=["1:1","2:3","3:2","3:4","4:3","4:5","5:4","9:16","16:9","21:9"]
VALID_IMAGE_SIZES=["512px","1K","2K","4K","0.5K"]
def _dims(ar,isz):
    isz=normalize_image_size(isz)
    le={"0.5K":512,"512px":512,"1K":1024,"2K":1536,"4K":2048}.get(isz,1024)
    w,h=[int(x) for x in ar.split(":")]
    if w>=h: W,H=le,round(le*h/w)
    else: H,W=le,round(le*w/h)
    return max(256,(W//8)*8),max(256,(H//8)*8)
def _placeholder(p,w,h):
    rng=random.Random(hash(p)&0xFFFFFFFF)
    img=Image.new("RGB",(w,h),(9,12,24))
    d=ImageDraw.Draw(img,"RGBA")
    pal=[(28,38,70),(52,67,105),(121,33,48),(186,137,64),(30,90,96),(88,20,60)]
    for i in range(30):
        c=pal[i%len(pal)]+(rng.randint(50,140),)
        x0,y0=rng.randint(-w//4,w),rng.randint(-h//4,h)
        r=rng.randint(max(60,w//10),max(160,w//3))
        d.ellipse((x0-r,y0-r,x0+r,y0+r),fill=c)
    img=img.filter(ImageFilter.GaussianBlur(radius=max(8,w//100)))
    d=ImageDraw.Draw(img,"RGBA")
    for i in range(60): d.rectangle((i,i,w-i,h-i),outline=(0,0,0,max(0,140-int(2.5*i))),width=2)
    try: fb=ImageFont.truetype("DejaVuSans-Bold.ttf",max(28,w//22));fs=ImageFont.truetype("DejaVuSans.ttf",max(14,w//50))
    except: fb=fs=ImageFont.load_default()
    pad=max(24,w//30); bh=max(100,h//5)
    d.rounded_rectangle((pad,h-bh-pad,w-pad,h-pad),radius=18,fill=(0,0,0,130),outline=(200,160,80,200),width=2)
    d.text((pad*1.6,h-bh-pad+18),"ComfyUI",fill=(240,220,170,255),font=fb)
    d.text((pad*1.6,h-bh-pad+60),p[:120],fill=(230,235,245,220),font=fs)
    buf=io.BytesIO();img.save(buf,format="PNG");return buf.getvalue()
def generate(prompt,aspect_ratio="1:1",image_size="1K",output_dir=None,filename=None,model=None,max_retries=MAX_RETRIES):
    image_size=normalize_image_size(image_size)
    w,h=_dims(aspect_ratio,image_size)
    path=resolve_output_path(prompt,output_dir,filename,".png")
    print(f"[ComfyUI] {w}x{h} {prompt[:80]}")
    return save_image_bytes(_placeholder(prompt,w,h),path)