Ë
    ûUAiä  ã                   óL   — d dl mZ d dlmZ d dlZdZdZdZddd	œd
ede	defd„Z
y)é    )ÚPath)ÚImageNéõ   g{®Gáz´?é   )éÏ   r   r   T)Ú
target_rgbÚinplaceÚ
image_pathr	   Úreturnc          
      óh  — t        j                  | «      j                  d«      }|j                  \  }}t	        j
                  |«      j                  t        j                  «      }t        t        ||«      t        z  «      }t	        j                  |d|…dd…dd…f   j                  dd«      || d…dd…dd…f   j                  dd«      |dd…d|…dd…f   j                  dd«      |dd…| d…dd…f   j                  dd«      g«      }t	        j                  |t        k\  d¬«      }	|	j                  «       }
|
dk  r| S t        j                   j#                  |dz
  d	¬«      }|t$        k  }t	        j&                  |t        j                  ¬
«      ||<   t        j(                  |j                  t        j*                  «      d«      }|r|j-                  | |j.                  ¬«       | S | j1                  | j2                  › d| j4                  › «      }|j-                  ||j.                  ¬«       |S )a2  
    Detects near-white surrounding background and recolors it
    to a soft off-white / grayish-white.

    - Only operates if edges are predominantly white
    - Modifies background only, not subject
    - Safe for illustrations, watercolor art, scans

    Returns image path (same path if inplace).
    ÚRGBNéÿÿÿÿé   é   )Úaxisgq=
×£pí?éÿ   é   )Údtype)ÚformatÚ_bg)r   ÚopenÚconvertÚsizeÚnpÚasarrayÚastypeÚint16ÚintÚminÚEDGE_SAMPLE_RATIOÚconcatenateÚreshapeÚallÚWHITE_THRESHOLDÚmeanÚlinalgÚnormÚBACKGROUND_DISTANCEÚarrayÚ	fromarrayÚuint8Úsaver   Ú	with_nameÚstemÚsuffix)r
   r   r	   ÚimgÚwÚhÚarrÚedge_pxÚsamplesÚis_whiteÚwhite_ratioÚdist_from_whiteÚbg_maskÚout_imgÚout_paths                  úL/var/www/html/story-book/Story-Book-python-api/app/utils/image_background.pyÚnormalize_white_backgroundr=      sÜ  € ô" *‰*ZÓ
 ×
(Ñ
(¨Ó
/€CØ8‰8D€A€qÜ
*‰*S‹/×
 Ñ
 ¤§¡Ó
*€Cä”#a˜“)Ô/Ñ/Ó0€Gô n‰nØˆHˆWˆH’ašˆNÑ×#Ñ# B¨Ó*ØˆWˆH‰I’qš!ˆOÑ×$Ñ$ R¨Ó+ØŠAˆxˆxšˆNÑ×#Ñ# B¨Ó*ØŠAˆx‰yš!ˆOÑ×$Ñ$ R¨Ó+ð	ó €Gô v‰vg¤Ñ0°qÔ9€HØ—-‘-“/€Kð TÒØÐô —i‘i—n‘n S¨3¡Y°QnÓ7€Oð Ô!4Ñ4€Gô —8‘8˜J¬b¯h©hÔ7€CˆLäo‰o˜cŸj™j¬¯©Ó2°EÓ:€GáØ‰Z¨¯
©
ˆÔ3ØÐà×#Ñ# z§¡Ð&7°s¸:×;LÑ;LÐ:MÐ$NÓO€HØ‡LL #§*¡*€LÔ-Ø€Oó    )Úpathlibr   ÚPILr   Únumpyr   r$   r    r(   Úboolr=   © r>   r<   Ú<module>rD      sJ   ðÝ Ý Û ð €ØÐ ØÐ ð Øò	8Øð8ð ð	8ð
 
ô8r>   