Sélectionner une révision Git
-
Yann LUMIA a rédigéYann LUMIA a rédigé
adacher.py 4,63 Kio
#!/usr/bin/python3
import argparse
import math
import tempfile
import sys
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
##### Argument parsing
parser = argparse.ArgumentParser(
prog='Adacher',
description='Generate beautiful adachers')
parser.add_argument('text')
parser.add_argument('--imgBase', type=str, default="adachi")
parser.add_argument('--nobox', action='store_true', default=False)
parser.add_argument('--nowrap', action='store_true', default=False)
parser.add_argument('--boxtop', type=float, default=0.65)
parser.add_argument('--boxheight', type=float, default=0.20)
parser.add_argument('--textVertMargin', type=int, default=5)
parser.add_argument('--textHoriMargin', type=int, default=10)
parser.add_argument('--fontPath', type=str, default="comic.ttf")
parser.add_argument('--textCol', type=str, default="ff0000")
parser.add_argument('--boxCol', type=str, default="ffffff")
parser.add_argument('--bgCol', type=str, default="ffffff")
parser.add_argument('--align', type=str, default="center")
args = parser.parse_args()
args.fontPath = "comic.ttf"
imgBases = {
"adachi": "adachi_white.png",
"adacher": "adacher.png"
}
if (args.imgBase in imgBases):
args.imgBase = imgBases[args.imgBase]
else:
print("Wrong base image")
sys.exit(1)
withBox = not args.nobox
withWrap = not args.nowrap
def hexToCol(st):
return tuple(int(st[i:i+2], 16) for i in (0, 2, 4))
textCol = hexToCol(args.textCol)
boxCol = hexToCol(args.boxCol)
bgCol = hexToCol(args.bgCol)
##### Function to calculate font size and line breaks
def growFontToBox(draw, fontPath, text, width, height, wrap=False):
fontsize = 1
font = ImageFont.truetype(fontPath, fontsize)
fontLeft, fontTop, fontRight, fontBottom = font.getbbox(text)
if not wrap:
newSize = fontsize
while fontRight-fontLeft < width and fontBottom-fontTop < height:
# iterate until the text size is just larger than the criteria
newSize += 1
font = ImageFont.truetype(fontPath, newSize)
fontLeft, fontTop, fontRight, fontBottom = font.getbbox(text)
newText = text
newSize -= 1
else:
newText = text
while True:
#print(fontsize)
fontsize += 1
font = ImageFont.truetype(fontPath, fontsize)
fontLeft, fontTop, fontRight, fontBottom = draw.multiline_textbbox((0,0), text, font, align=args.align)
if fontBottom-fontTop >= height:
# Doesn't fit vertically, stop now
break
elif fontRight-fontLeft >= width:
# Doesn't fit horizontally, try to cut the line
textArray = text.split()
tmpText = textArray.pop(0)
while textArray:
fontLeft, fontTop, fontRight, fontBottom = draw.multiline_textbbox((0,0), tmpText + " " + textArray[0], font, align=args.align)
if fontRight-fontLeft > width:
tmpText += "\n" + textArray.pop(0)
else:
tmpText += " " + textArray.pop(0)
fontLeft, fontTop, fontRight, fontBottom = draw.multiline_textbbox((0,0), tmpText, font, align=args.align)
if fontRight-fontLeft <= width and fontBottom-fontTop <= height:
newText = tmpText
newSize = fontsize
else:
break
else:
newSize = fontsize
newText = text
return newText, newSize
imgBase = Image.open(args.imgBase)
##### Add white box
boxTop = args.boxtop * imgBase.size[1]
boxHeight = args.boxheight * imgBase.size[1]
boxBot = math.floor(boxTop + boxHeight)
img = Image.new("RGBA", (imgBase.size[0], boxBot))
width,height = img.size
draw = ImageDraw.Draw(img)
draw.rectangle([0, 0, width, boxBot], bgCol)
img.alpha_composite(imgBase)
draw.rectangle([0, boxTop, width, boxBot], boxCol)
newText, fontsize = growFontToBox(draw, args.fontPath, args.text, img.size[0] - 2*args.textHoriMargin, boxHeight - 2*args.textVertMargin, withWrap)
font = ImageFont.truetype(args.fontPath, fontsize)
fontLeft, fontTop, fontRight, fontBottom = draw.multiline_textbbox((0,0), newText, font, align=args.align)
draw.text((
(width - (fontRight - fontLeft))/2,
boxTop + (boxHeight - (fontBottom- fontTop))/2 - fontTop
),
newText,
textCol,
font=font,
align=args.align
)
finalHeight = 200
finalWidth = finalHeight * width // boxBot
img = img.resize((finalWidth, finalHeight), Image.Resampling.LANCZOS)
img.save("tmpAdacher.png", format="PNG")
print(str(finalWidth) + "," + str(finalHeight))