Albator2570 commited on
Commit
c709870
·
verified ·
1 Parent(s): ee24687

Upload backend_comfyui.py

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