aboutsummaryrefslogtreecommitdiff
path: root/modules/images.py
diff options
context:
space:
mode:
Diffstat (limited to 'modules/images.py')
-rw-r--r--modules/images.py77
1 files changed, 77 insertions, 0 deletions
diff --git a/modules/images.py b/modules/images.py
index c0a90676..e62eec8e 100644
--- a/modules/images.py
+++ b/modules/images.py
@@ -463,3 +463,80 @@ def save_image(image, path, basename, seed=None, prompt=None, extension='png', i
txt_fullfn = None
return fullfn, txt_fullfn
+
+def addCaptionLines(lines,image,initialx,textfont):
+ draw = ImageDraw.Draw(image)
+ hstart =initialx
+ for fill,line in lines:
+ fontSize = 32
+ font = ImageFont.truetype(textfont, fontSize)
+ _,_,w, h = draw.textbbox((0,0),line,font=font)
+ fontSize = min( int(fontSize * ((image.size[0]-35)/w) ), 28)
+ font = ImageFont.truetype(textfont, fontSize)
+ _,_,w,h = draw.textbbox((0,0),line,font=font)
+ draw.text(((image.size[0]-w)/2,hstart), line, font=font, fill=fill)
+ hstart += h
+ return hstart
+
+def captionImge(image,prelines,postlines,background=(51, 51, 51),font=None):
+ if font is None:
+ try:
+ font = ImageFont.truetype(opts.font or Roboto, fontsize)
+ font = opts.font or Roboto
+ except Exception:
+ font = Roboto
+
+ sampleImage = image
+ background = Image.new("RGBA", (sampleImage.size[0],sampleImage.size[1]+1024), background)
+ hoffset = addCaptionLines(prelines,background,5,font)+16
+ background.paste(sampleImage,(0,hoffset))
+ hoffset = hoffset+sampleImage.size[1]+8
+ hoffset = addCaptionLines(postlines,background,hoffset,font)
+ background = background.crop((0,0,sampleImage.size[0],hoffset+8))
+ return background
+
+def captionImageOverlay(srcimage,title,footerLeft,footerMid,footerRight,textfont=None):
+ from math import cos
+
+ image = srcimage.copy()
+
+ if textfont is None:
+ try:
+ textfont = ImageFont.truetype(opts.font or Roboto, fontsize)
+ textfont = opts.font or Roboto
+ except Exception:
+ textfont = Roboto
+
+ factor = 1.5
+ gradient = Image.new('RGBA', (1,image.size[1]), color=(0,0,0,0))
+ for y in range(image.size[1]):
+ mag = 1-cos(y/image.size[1]*factor)
+ mag = max(mag,1-cos((image.size[1]-y)/image.size[1]*factor*1.1))
+ gradient.putpixel((0, y), (0,0,0,int(mag*255)))
+ image = Image.alpha_composite(image.convert('RGBA'), gradient.resize(image.size))
+
+ draw = ImageDraw.Draw(image)
+ fontSize = 32
+ font = ImageFont.truetype(textfont, fontSize)
+ padding = 10
+
+ _,_,w, h = draw.textbbox((0,0),title,font=font)
+ fontSize = min( int(fontSize * (((image.size[0]*0.75)-(padding*4))/w) ), 72)
+ font = ImageFont.truetype(textfont, fontSize)
+ _,_,w,h = draw.textbbox((0,0),title,font=font)
+ draw.text((padding,padding), title, anchor='lt', font=font, fill=(255,255,255,230))
+
+ _,_,w, h = draw.textbbox((0,0),footerLeft,font=font)
+ fontSizeleft = min( int(fontSize * (((image.size[0]/3)-(padding))/w) ), 72)
+ _,_,w, h = draw.textbbox((0,0),footerMid,font=font)
+ fontSizemid = min( int(fontSize * (((image.size[0]/3)-(padding))/w) ), 72)
+ _,_,w, h = draw.textbbox((0,0),footerRight,font=font)
+ fontSizeright = min( int(fontSize * (((image.size[0]/3)-(padding))/w) ), 72)
+
+ font = ImageFont.truetype(textfont, min(fontSizeleft,fontSizemid,fontSizeright))
+
+ draw.text((padding,image.size[1]-padding), footerLeft, anchor='ls', font=font, fill=(255,255,255,230))
+ draw.text((image.size[0]/2,image.size[1]-padding), footerMid, anchor='ms', font=font, fill=(255,255,255,230))
+ draw.text((image.size[0]-padding,image.size[1]-padding), footerRight, anchor='rs', font=font, fill=(255,255,255,230))
+
+ return image