add caption image with overlay
This commit is contained in:
parent
01fd9cf0d2
commit
0ac3a07eec
1 changed files with 46 additions and 0 deletions
|
@ -459,3 +459,49 @@ def captionImge(image,prelines,postlines,background=(51, 51, 51),font=None):
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue