22
33from db .schema .chat_message_attachment import ChatMessageAttachment
44from di .di import DI
5+ from features .images .aspect_ratio_utils import validate_aspect_ratio
56from features .images .image_background_remover import ImageBackgroundRemover
67from features .images .image_contents_restorer import ImageContentsRestorer
78from features .images .image_editor import ImageEditor
@@ -37,13 +38,15 @@ def resolve(operation: str) -> "ChatImagingService.Operation":
3738 __attachments : list [ChatMessageAttachment ]
3839 __operation : Operation
3940 __operation_guidance : str | None
41+ __aspect_ratio : str
4042 __di : DI
4143
4244 def __init__ (
4345 self ,
4446 attachment_ids : list [str ],
4547 operation_name : str ,
4648 operation_guidance : str | None ,
49+ aspect_ratio : str | None ,
4750 di : DI ,
4851 ):
4952 if not attachment_ids :
@@ -52,6 +55,7 @@ def __init__(
5255 self .__attachments = self .__di .platform_bot_sdk ().refresh_attachments_by_ids (attachment_ids )
5356 self .__operation = ChatImagingService .Operation .resolve (operation_name )
5457 self .__operation_guidance = operation_guidance
58+ self .__aspect_ratio = validate_aspect_ratio (aspect_ratio , ImageEditor .DEFAULT_ASPECT_RATIO )
5559
5660 def __remove_background (self ) -> tuple [Result , URLList , ErrorList ]:
5761 log .t (f"Removing background from { len (self .__attachments )} images" )
@@ -163,6 +167,7 @@ def __edit_image(self) -> tuple[Result, URLList, ErrorList]:
163167 configured_tool = configured_tool ,
164168 context = self .__operation_guidance ,
165169 mime_type = attachment .mime_type ,
170+ aspect_ratio = self .__aspect_ratio ,
166171 )
167172 editing_result = editor .execute ()
168173 if editor .error :
@@ -194,50 +199,50 @@ def execute(self) -> tuple[Result, list[dict[str, str | None]]]:
194199 if self .__operation == ChatImagingService .Operation .remove_background :
195200 result , urls , errors = self .__remove_background ()
196201 urls = self .__clean_urls (urls )
197- output : list [dict [str , str | None ]] = []
202+ output_removal_urls : list [dict [str , str | None ]] = []
198203 for image_url , error in zip (urls , errors ):
199204 if image_url is None :
200- output .append ({"url" : None , "error" : error })
205+ output_removal_urls .append ({"url" : None , "error" : error })
201206 continue
202207 external_id = str (invoker_chat .external_id )
203208 log .t (f"Sending edited image to chat '{ external_id } '" )
204209 self .__di .platform_bot_sdk ().send_document (external_id , image_url , thumbnail = image_url )
205210 self .__di .platform_bot_sdk ().send_photo (external_id , image_url , caption = "📸" )
206211 log .t ("Image edited and sent successfully" )
207- output .append ({"url" : image_url , "error" : None , "status" : "delivered" })
208- return result , output
212+ output_removal_urls .append ({"url" : image_url , "error" : None , "status" : "delivered" })
213+ return result , output_removal_urls
209214
210215 elif self .__operation == ChatImagingService .Operation .restore_image :
211216 result , urls , errors = self .__restore_image ()
212217 urls = self .__clean_urls (urls )
213- output : list [dict [str , str | None ]] = []
218+ output_restoration_urls : list [dict [str , str | None ]] = []
214219 for image_url , error in zip (urls , errors ):
215220 if image_url is None :
216- output .append ({"url" : None , "error" : error })
221+ output_restoration_urls .append ({"url" : None , "error" : error })
217222 continue
218223 external_id = str (invoker_chat .external_id )
219224 log .t (f"Sending restored image to chat '{ external_id } ': { image_url } " )
220225 self .__di .platform_bot_sdk ().send_document (external_id , image_url , thumbnail = image_url )
221226 self .__di .platform_bot_sdk ().send_photo (external_id , image_url , caption = "📸" )
222227 log .t ("Image restored and sent successfully" )
223- output .append ({"url" : image_url , "error" : None , "status" : "delivered" })
224- return result , output
228+ output_restoration_urls .append ({"url" : image_url , "error" : None , "status" : "delivered" })
229+ return result , output_restoration_urls
225230
226231 elif self .__operation == ChatImagingService .Operation .edit_image :
227232 result , urls , errors = self .__edit_image ()
228233 urls = self .__clean_urls (urls )
229- output : list [dict [str , str | None ]] = []
234+ output_editing_urls : list [dict [str , str | None ]] = []
230235 for image_url , error in zip (urls , errors ):
231236 if image_url is None :
232- output .append ({"url" : None , "error" : error })
237+ output_editing_urls .append ({"url" : None , "error" : error })
233238 continue
234239 external_id = str (invoker_chat .external_id )
235240 log .t (f"Sending edited image to chat '{ external_id } ': { image_url } " )
236241 self .__di .platform_bot_sdk ().send_document (external_id , image_url , thumbnail = image_url )
237242 self .__di .platform_bot_sdk ().send_photo (external_id , image_url , caption = "📸" )
238243 log .t ("Image edited and sent successfully" )
239- output .append ({"url" : image_url , "error" : None , "status" : "delivered" })
240- return result , output
244+ output_editing_urls .append ({"url" : image_url , "error" : None , "status" : "delivered" })
245+ return result , output_editing_urls
241246
242247 else :
243248 raise ValueError (f"Unknown operation '{ self .__operation .value } '" )
0 commit comments