from PIL import Image, ImageDraw, ImageFont, ImageFilter

src='/home/spazio/.hermes/cache/images/img_5384b6302ca1.jpg'
out='/home/spazio/cosmos_luminosity_bold_mockup.jpg'
im=Image.open(src).convert('RGB')
W,H=im.size

# Deep local gradients to suppress the prior delicate typography while preserving the portrait.
overlay=Image.new('RGBA',(W,H),(0,0,0,0))
p=overlay.load()
for y in range(H):
    for x in range(W):
        a=0
        # left editorial text field
        if x < 760 and y < 900:
            horiz=max(0,1-x/780)
            vert=max(0,1-y/1000)
            # Strong enough to fully suppress the original delicate copy,
            # feathered rather than boxed.
            a=max(a,int(252*horiz*(0.90+0.10*vert)))
        # top strip
        if y < 110:
            a=max(a,int(235*(1-y/145)))
        # lower copy field
        if y > 970:
            a=max(a,int(175*((y-970)/(H-970))))
        p[x,y]=(7,3,16,a)
im=Image.alpha_composite(im.convert('RGBA'),overlay)
d=ImageDraw.Draw(im)

bold='/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf'
regular='/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf'
WHITE=(250,248,244,255)
VIOLET=(143,78,222,255)
YELLOW=(246,211,67,255)
GOLD=(214,175,84,255)

# utility functions
def fit_font(text,max_width,start=96,min_size=30,path=bold):
    for size in range(start,min_size-1,-1):
        f=ImageFont.truetype(path,size)
        if d.textbbox((0,0),text,font=f)[2] <= max_width:
            return f
    return ImageFont.truetype(path,min_size)

def draw_text_shadow(x,y,text,font,fill,sw=0):
    d.text((x+2,y+3),text,font=font,fill=(0,0,0,130))
    d.text((x,y),text,font=font,fill=fill)

# Top metadata
small=ImageFont.truetype(bold,24)
d.text((56,48),'ВОПРОС 03  /  ВЗЛОМ ВНИМАНИЯ',font=small,fill=GOLD)
d.line((56,88,310,88),fill=GOLD,width=4)
d.text((945,48),'04 / 07',font=ImageFont.truetype(bold,20),fill=WHITE,anchor='ra')

# Main statement: oversized and assertive, clear of the face.
lines=[
    ('КАКОЙ',WHITE,88),
    ('ВАРИАНТ',VIOLET,94),
    ('ТЫ ДАЖЕ',WHITE,86),
    ('НЕ ДОПУСКАЕШЬ?',YELLOW,76),
]
y=150
for text,color,size in lines:
    font=fit_font(text,585,size,38)
    draw_text_shadow(54,y,text,font,color)
    bbox=d.textbbox((54,y),text,font=font)
    y=bbox[3]-3

# Strong gold marker
marker_y=y+24
d.rectangle((56,marker_y,166,marker_y+9),fill=GOLD)

# Supporting punchline at the bottom-left, wrapped manually.
support=['СТАРЫЙ ВЫВОД УЖЕ','ОТРЕЗАЛ ТЕБЕ ОБЗОР.']
sy=1088
for line in support:
    f=fit_font(line,770,47,28)
    draw_text_shadow(56,sy,line,f,WHITE,1)
    sy += 58

# Brand signature
d.text((1025,1288),'COSMOS LUMINOSITY',font=ImageFont.truetype(bold,19),fill=GOLD,anchor='ra')
d.text((56,1288),'A LIVING INTERFACE OF ATTENTION',font=ImageFont.truetype(regular,15),fill=(255,255,255,205),anchor='la')

im.convert('RGB').save(out,quality=95,subsampling=0)
print(out)
print(Image.open(out).size)
