+
    {Dìjî  ã                   ó`   € R t ^ RIt^ RIt^ RIt^ RIt^ RIt^ RIHt ^ RI	H
t
 RtR tR tR tR# )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                ón  €  V P                   '       d¸   \        P                  ! V P                   P                  4      P	                  R4      p\        P
                  ! RVP                  R4      pVP                  WP                  R4      R7       \        P                  ! 4       pVP                  VRR7       VP                  4       RR3#  \        V P                  P                  R	4      ;_uu_ 4       pVP                  4       R
R3uuRRR4       #   \         d     LSi ; i  + '       g   i     R# ; i)zZThe cleaned garment (no model, no background) on white; falls back to the original upload.ÚRGBAÚRGBÚA)ÚmaskÚPNG)Úformatzgarment.pngz	image/pngÚrbzgarment.jpgú
image/jpegN)éÿ   r   r   )Úimager   ÚopenÚpathÚconvertÚnewÚsizeÚpasteÚ
getchannelÚioÚBytesIOÚsaveÚgetvalueÚ	ExceptionÚ	raw_imageÚread)ÚdressÚimgÚwhiteÚbufÚhandles   &    Ú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#   ‚D ”B6D Ã4D#ÄD ÄD Ä#D4	c                 ó�   € V P                   R ,          w  r\        W!,
          4      R\        W!4      ,          8  d   R# W8”  d   R# R# ):Né   Ngš™™™™™¹?Ú	1024x1024Ú	1024x1536Ú	1536x1024)ÚshapeÚabsÚmax)Ú	photo_bgrÚhÚws   &  r"   Ú	_size_forr/   +   s:   € Ø�?‰?˜2Õ�D€AÜ
ˆ1�5ƒz�Cœ#˜a›)•OÔ#ÙØœ%ˆ;Ð0 [Ð0ó    c                óØ  € \         P                  pV'       g   \        R4      h\        P                  ! RV \        P
                  ^_.4      w  r4V'       g   \        R4      h\        V4      w  rVp\        P                  ! \         P                  P                  R4      R,           RRV 2/RR	VP                  4       R
33RWeV33.R\         P                  R\        R\        V 4      R\         P                  /\         P                   R7      pVP"                  ^È8w  dh    VP%                  4       P'                  R/ 4      P'                  RR4      p	\        RVP"                   RT	;'       g    VP*                  R,           24      hVP%                  4       P'                  R4      ;'       g    . p
V
'       d   V
^ ,          P'                  R4      '       g   \        R4      h\        P,                  ! \.        P0                  ! \2        P4                  ! V
^ ,          R,          4      \.        P6                  4      \        P8                  4      pVf   \        R4      hV#   \(         d    Rp	 ELi ; i)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.jpgz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é–   Nr9   Ú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ÚPROMPTr/   Ú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<   r9   r   s   &&          r"   Úrenderra   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ð
ð
 ”X×0Ñ0Ø”fØ”I˜jÓ)Ø”x×4Ñ4ð	
ô ×'Ñ'ô€Dð ×Ñ˜3Ôð	Ø—i‘i“k—o‘o g¨rÓ2×6Ñ6°yÀ"ÓEˆGô Ð-¨d×.>Ñ.>Ð-?¸rÀ7×CYÐ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   Ä0I ÉI)É(I))Ú__doc__rV   r   rB   ÚnumpyrT   rE   Údjango.confr   ÚPILr   rK   r#   r/   ra   © r0   r"   Ú<module>rg      s;   ðñó Û 	ã 
Û Û Ý  Ý ðKð ò:ò 1ô'r0   