#!/usr/bin/env python3
"""pad-logo.py <logo.png> <out.png> <frame-width> <frame-height> <size-slider-percent> <right-px> <bottom-px>
Adds transparent padding to a logo so that, with the Koldflux Add watermark tool set to "Bottom right" and the size slider at
<size-slider-percent>, the visible logo ends <right-px> from the right edge and <bottom-px> from the bottom edge of a
<frame-width> x <frame-height> video. The tool keeps a margin of 3% of the frame width (right) and 3% of the frame height
(bottom), so those 3% are subtracted. Needs Python 3 and Pillow. The tool's size slider runs from 5 to 50, so a slider above 50 is refused. Exit 2 on a bad input, an unwritable output path or a result over 20000 px."""
import math, sys
from PIL import Image

def die(msg):
    print(msg, file=sys.stderr); sys.exit(2)

if len(sys.argv) != 8: die(__doc__)
src, dst = sys.argv[1], sys.argv[2]
try:
    fw, fh, share, right, bottom = (float(x) for x in sys.argv[3:8])
except ValueError:
    die("frame size, slider, right and bottom must be numbers")
if not all(math.isfinite(v) for v in (fw, fh, share, right, bottom)): die("frame size, slider, right and bottom must be finite numbers")
if min(fw, fh, share) <= 0 or right < 0 or bottom < 0: die("frame size and slider must be above 0, distances not negative")
if share > 50: die("the tool's size slider stops at 50")
if max(fw, fh, right, bottom) > 20000: die("frame size and distances must be 20000 px or less")
try:
    im = Image.open(src).convert("RGBA")
except (OSError, ValueError) as e:
    die("cannot read an image from %s (%s)" % (src, e.__class__.__name__))
box = im.getchannel("A").getbbox()
if box is None: die("the logo is fully transparent: nothing to place")
im = im.crop(box)                                  # trim the transparent border the file came with
c, ch = im.size
k_out = share / 100 * fw                           # width of the whole padded file on the frame, in output px
pad_r = right - 0.03 * fw                          # padding needed on the right, in output px
pad_b = bottom - 0.03 * fh
if pad_r < 0 or pad_b < 0: die("the tool's own 3% margin is already larger than the distance you asked for: no padding needed")
if k_out <= pad_r: die("slider too small: the padding alone would be as wide as the file; raise the slider above %.1f" % (100 * pad_r / fw))
w_file = c / (1 - pad_r / k_out)                   # file width in file px: visible width + right padding
scale = k_out / w_file                             # output px per file px
h_file = ch + pad_b / scale
if max(w_file, h_file) > 20000: die("the padded file would be larger than 20000 px: lower the distances or raise the slider")
canvas = Image.new("RGBA", (round(w_file), round(h_file)), (0, 0, 0, 0))
canvas.paste(im, (0, 0))                           # logo at top left: the padding sits right of and below it
try:
    canvas.save(dst)
except (OSError, ValueError) as e:
    die("cannot write %s (%s)" % (dst, e.__class__.__name__))
print("saved %s: %dx%d px, visible logo about %.0f px wide on the frame" % (dst, canvas.width, canvas.height, c * scale))
