o
    Ék  ã                   @   s`   d Z ddlZddlZddlZddlmZ dd„ Zddd„Zd	d
„ Z	dd„ Z
edkr.e
ƒ  dS dS )a¤  Measure stroke weight (stem thickness) and ink coverage of a type block.

fontspec.py normalises candidates by x-height and then ranks them on width. That
cannot see weight: a too-heavy face simply needs more negative tracking and comes
out the same width. So width tells you the family and size, and this tells you
whether the weight is right.

Two numbers, both scale-free by dividing by cap height:

  cov   ink pixels / ink-bbox area. Rises with weight, but also with tracking
        (wider spaces) and with the number of counters.
  stem  median horizontal run length in the middle 50% of the ink bbox, i.e.
        the thickness of vertical stems, excluding serifs and horizontals.
        This is the one that tracks weight.

Compare a mock against a render:

  python3 tools/stroke.py --a mock.jpg --b shots/final.png --region 60,190,900,340
  python3 tools/stroke.py --a mock.jpg --b shots/final.png --region ... --invert
é    N)ÚImagec                 C   sd   t jt | ¡ d¡t jd�}|jdd�}|\}}}}	|||	d …||d …f }
|r.|
|kS |
|k S )NÚRGB©Údtypeé   )Úaxisé   )ÚnpÚasarrayr   ÚopenÚconvertÚfloat32Úmean)ÚpathÚregionÚthreshÚinvertÚarrÚlumÚx0Úy0Úx1Úy1Úsub© r   ú</home/ubuntu/.pi/agent/skills/mockup-to-html/tools/stroke.pyÚ	load_mask   s
   r   ©g      Ð?g      è?c                 C   sf  t  | ¡\}}t|ƒdkrdS t| ¡ ƒt| ¡ ƒ}}|| d }t||d |  ƒt||d |  ƒ}}g }	t||d ƒD ]K}
| |
  t j¡}| 	¡ sOq@t  
t  dg|dgf¡¡}tt  |dk¡d t  |dk¡d ƒD ]\}}|| }d|  k rƒ|d krŠn qo|	 |¡ qoq@|	r•tt  |	¡ƒnd}t| | ¡ |d …| ¡ | ¡ d …f  ¡ ƒ}||fS )aG  Median horizontal ink run inside the middle band of the ink's bbox.

    Restricting to the middle band drops baseline/top serifs, whose runs are
    horizontal and much longer than a stem. Runs longer than half the ink height
    are horizontals or joined letters, so they are excluded rather than skewing
    the median.
    r   )NNr   éÿÿÿÿg      à?N)r	   ÚwhereÚlenÚintÚminÚmaxÚrangeÚastypeÚint8ÚanyÚdiffÚconcatenateÚzipÚappendÚfloatÚmedianr   )ÚmaskÚbandÚysÚxsr   r   ÚhÚloÚhiÚrunsÚyÚrowÚdÚsÚeÚLÚstemÚcovr   r   r   Ú
stem_width%   s*   *.
€ý0r>   c           	      C   sn   t | |||ƒ}t |¡\}}t|ƒdkrd S t|ƒ\}}||t| ¡ | ¡  d ƒt| ¡ | ¡  d ƒdœS )Nr   r   )r<   r=   ÚcapÚw)r   r	   r   r    r>   r!   r#   r"   )	r   r   r   r   Úmr0   r1   r<   r=   r   r   r   ÚreportB   s   ÿrB   c                  C   sæ  t  ¡ } | jdddd� | jdd dd� | jddd	d� | jd
td dd� | jdddd� |  ¡ }dd„ |j d¡D ƒ}|jd u rïtj	t
 |j¡ d¡tjd�}||d |d d …|d |d d …f  ¡ }tj|dd�\}}|d d… |dd …  d }d|d }}	tdt|ƒd ƒD ]Q}
|d |
…  ¡ ||
d …  ¡ }}|dksª|dkr«q�|d |
… |d |
…   ¡ | }||
d … ||
d …   ¡ | }|| || d  }||krà|||
 }}	q�t|	ƒ|_td|jd›�ƒ t|j||j|jƒ}|d u �rt d¡ td d!›d"d#d$›d"d%d&›d"d'd&›d"d(d&›�	ƒ td)d!›d"|d# d*›d"|d+ d,›d"|d' d-›d"|d( d.›�	ƒ |j�sMtd/|d' |d#  d0›�ƒ d S t|j||j|jƒ}|d u �rat d1¡ td2d!›d"|d# d*›d"|d+ d,›d"|d' d-›d"|d( d.›�	ƒ |d' |d#  |d' |d#  }}td3|d0›d4|d0›d5|| d6›�ƒ || }|d7k�rºtd8d9|d  d›d:�ƒ n|d;k �rÍtd<d9d|  d›d=�ƒ ntd>ƒ |d( |d( }}td?|d6›d@|d6›dAd9|| d  dB›dC�ƒ d S )DNz--aTzreference image (the mockup))ÚrequiredÚhelpz--bzcomparison image (the render))ÚdefaultrD   z--regionzx0,y0,x1,y1z--threshz8ink threshold on luminance (default: Otsu on the region))ÚtyperE   rD   z--invertÚ
store_truezlight ink on dark ground)ÚactionrD   c                 S   s   g | ]}t |ƒ‘qS r   )r!   )Ú.0Úvr   r   r   Ú
<listcomp>U   s    zmain.<locals>.<listcomp>ú,r;   r   r   é   r   r   é@   )Úbinsr   zthreshold (Otsu) = z.0fz7no ink found in region of --a; widen or change --threshÚ Ú8sú r?   z>5sÚwidthz>7sr<   r=   ÚmockÚ5dr@   Ú7dz7.2fz7.3fz
stem/cap = z.4fzno ink found in region of --bÚrenderz
stem/cap  mock z
   render z   render/mock z.3fgìQ¸…ëñ?zverdict: too heavy by ~éd   u   % â€” try a lighter weightg)\�Âõ(ì?zverdict: too light by ~u   % â€” try a heavier weightz"verdict: weight matches within 12%zcoverage mock z render z (z+.0fz%))ÚargparseÚArgumentParserÚadd_argumentr,   Ú
parse_argsr   Úsplitr   r	   r
   r   r   Úar   r   ÚravelÚ	histogramr$   r    ÚsumÚprintrB   r   ÚsysÚexitÚb)Úapr^   r   r   r   ÚhistÚedgesÚcentresÚbestÚcutÚiÚw0Úw1Úm0Úm1rJ   ÚraÚrbÚmaÚmbr8   ÚcaÚcbr   r   r   ÚmainL   sd   
ÿ
0"  €


.>

>"$

2rw   Ú__main__)r   )Ú__doc__rY   rc   Únumpyr	   ÚPILr   r   r>   rB   rw   Ú__name__r   r   r   r   Ú<module>   s   

;
ÿ