Ë
    {Dìjî  ã                   ó^   — d Z ddlZddlZddlZddlZddlZddlmZ ddl	m
Z
 dZd„ Zd„ Zd„ Zy)	aÎ  
"openai" engine: edits the customer photo with an OpenAI image model (paid, per image).

- The customer photo and the cleaned garment image are sent to OpenAI (a third party): get consent.
- Needs an API key with billing (OPENAI_API_KEY in .env). The key is never logged.
- Unlike FASHN (a dedicated try-on model) this is a general image editor: it may change small details
  of the face, background or garment. Test it on your own photos before relying on it.
é    N)Úsettings)ÚImagea¸  Image 1 is a photo of a customer. Image 2 shows a garment on a plain white background. Edit image 1 so the customer is wearing the garment from image 2. Keep the customer's face, identity, skin tone, hair, body shape, pose and the background exactly as they are; change only the clothing. The garment must look realistic with natural folds and a correct fit, and keep its colour, pattern and details. Do not add text, logos or extra people.c                 ó8  — 	 | j                   rµt        j                  | j                   j                  «      j	                  d«      }t        j
                  d|j                  d«      }|j                  ||j                  d«      ¬«       t        j                  «       }|j                  |d¬«       |j                  «       dd	fS 	 t        | j                  j                  d
«      5 }|j                  «       ddfcddd«       S # t        $ r Y ŒHw xY w# 1 sw Y   yxY w)zZThe cleaned garment (no model, no background) on white; falls back to the original upload.ÚRGBAÚRGB)éÿ   r   r   ÚA)ÚmaskÚPNG)Úformatzgarment.pngz	image/pngÚrbzgarment.jpgú
image/jpegN)Úimager   ÚopenÚpathÚconvertÚnewÚsizeÚpasteÚ
getchannelÚioÚBytesIOÚsaveÚgetvalueÚ	ExceptionÚ	raw_imageÚread)ÚdressÚimgÚwhiteÚbufÚhandles        úWC:\Users\Stabi\Downloads\virtual-tryon(1)\virtual-tryon\tryon\services\engine_openai.pyÚgarment_on_whiter$      sÞ   € ð	Ø�;Š;Ü—*‘*˜UŸ[™[×-Ñ-Ó.×6Ñ6°vÓ>ˆCÜ—I‘I˜e S§X¡X¨Ó?ˆEØ�K‰K˜ #§.¡.°Ó"5ˆKÔ6Ü—*‘*“,ˆCØ�J‰J�s 5ˆJÔ)Ø—<‘<“> =°+Ð=Ð=ð ô 
ˆe�o‰o×"Ñ" DÔ	)¨VØ�{‰{‹}˜m¨\Ð9÷ 
*Ñ	)øô ò Ùðúç	)Ð	)ús   ‚C D Ã$DÄ	DÄDÄDc                 óv   — | j                   d d \  }}t        ||z
  «      dt        ||«      z  k  ry||kD  rdS dS )Né   gš™™™™™¹?Ú	1024x1024Ú	1024x1536Ú	1536x1024)ÚshapeÚabsÚmax)Ú	photo_bgrÚhÚws      r#   Ú	_size_forr0   +   sD   € Ø�?‰?˜2˜AÐ�D€A€qÜ
ˆ1ˆq‰5ƒz�Cœ#˜a ›)‘OÒ#ØØ˜aš%ˆ;Ð0 [Ð0ó    c           
      óP  — t         j                  }|st        d«      ‚t        j                  d| t        j
                  dg«      \  }}|st        d«      ‚t        |«      \  }}}t        j                  t         j                  j                  d«      dz   dd|› �id	d
|j                  «       dffd	|||ffgt         j                  t        t        | «      t         j                  dœt         j                   ¬«      }|j"                  dk7  r]	 |j%                  «       j'                  di «      j'                  dd«      }	t        d|j"                  › d|	xs |j*                  dd › �«      ‚|j%                  «       j'                  d«      xs g }
|
r|
d   j'                  d«      st        d«      ‚t        j,                  t/        j0                  t3        j4                  |
d   d   «      t.        j6                  «      t        j8                  «      }|€t        d«      ‚|S # t(        $ r d}	Y Œëw xY w)z_Return the edited photo as a BGR image. Raises RuntimeError with a readable message on failure.z!OPENAI_API_KEY is not set in .envz.jpgé_   zCould not encode the photo.Ú/z/images/editsÚAuthorizationzBearer zimage[]zcustomer.jpgr   )ÚmodelÚpromptr   Úquality)ÚheadersÚfilesÚdataÚtimeoutéÈ   ÚerrorÚmessageÚ zOpenAI answered z: Né–   r;   r   Úb64_jsonzOpenAI returned no image.z$OpenAI returned an unreadable image.)r   ÚOPENAI_API_KEYÚRuntimeErrorÚcv2ÚimencodeÚIMWRITE_JPEG_QUALITYr$   ÚrequestsÚpostÚOPENAI_BASE_URLÚrstripÚtobytesÚOPENAI_IMAGE_MODELÚPROMPTr0   ÚOPENAI_IMAGE_QUALITYÚOPENAI_TIMEOUTÚstatus_codeÚjsonÚgetÚ
ValueErrorÚreasonÚimdecodeÚnpÚ
frombufferÚbase64Ú	b64decodeÚuint8ÚIMREAD_COLOR)Ú
person_bgrr   ÚkeyÚokr!   Úgarment_bytesÚgarment_nameÚgarment_typeÚrespr?   r;   r   s               r#   Úrenderrd   2   sô  € ä
×
!Ñ
!€CÙÜÐ>Ó?Ð?ä�l‰l˜6 :´×0HÑ0HÈ"Ð/MÓN�G€BˆÙÜÐ8Ó9Ð9Ü0@ÀÓ0GÑ-€M�< ä�=‰=Ü× Ñ ×'Ñ'¨Ó,¨Ñ>Ø  G¨C¨5 /Ð2à˜¨¯©«¸ÐEÐFØ˜ }°lÐCÐDð
ô
 ×0Ñ0ÜÜ˜jÓ)Ü×4Ñ4ñ	
ô ×'Ñ'ô€Dð ×Ñ˜3Òð	Ø—i‘i“k—o‘o g¨rÓ2×6Ñ6°yÀ"ÓEˆGô Ð-¨d×.>Ñ.>Ð-?¸rÀ7ÒCYÈdÏkÉkÐ[_Ð\_ÐB`ÐAaÐbÓcÐcà�9‰9‹;�?‰?˜6Ó"Ò( b€DÙ�t˜A‘w—{‘{ :Ô.ÜÐ6Ó7Ð7Ü�L‰LœŸ™¤v×'7Ñ'7¸¸Q¹À
Ñ8KÓ'LÌbÏhÉhÓWÔY\×YiÑYiÓj€EØ€}ÜÐAÓBÐBØ€Løô ò 	ØŠGð	ús   Ä	0H ÈH%È$H%)Ú__doc__rY   r   rE   ÚnumpyrW   rH   Údjango.confr   ÚPILr   rN   r$   r0   rd   © r1   r#   Ú<module>rj      s;   ðñó Û 	ã 
Û Û Ý  Ý ðKð ò:ò 1ó'r1   