@@ -755,10 +755,29 @@ def upload_one(seq: int, url: str) -> bool:
755755 try :
756756 screenshot_data = screenshots [seq ]
757757 base64_str = screenshot_data ["base64" ]
758- format_str = screenshot_data .get ("format" , "jpeg" )
758+ format_str = str (screenshot_data .get ("format" , "jpeg" ) or "jpeg" ).lower ()
759+ content_type = "image/jpeg"
759760
760761 # Decode base64 to image bytes
761762 image_bytes = base64 .b64decode (base64_str )
763+ if format_str not in ("jpeg" , "jpg" ):
764+ # Convert to JPEG to match presigned content-type.
765+ try :
766+ from io import BytesIO
767+
768+ from PIL import Image
769+
770+ with Image .open (BytesIO (image_bytes )) as img :
771+ rgb = img .convert ("RGB" )
772+ out = BytesIO ()
773+ rgb .save (out , format = "JPEG" , quality = 80 )
774+ image_bytes = out .getvalue ()
775+ format_str = "jpeg"
776+ except Exception as e :
777+ if self .logger :
778+ self .logger .warning (
779+ f"Screenshot { seq } format '{ format_str } ' could not be converted to JPEG: { e } "
780+ )
762781 image_size = len (image_bytes )
763782
764783 # Update total size
@@ -769,7 +788,7 @@ def upload_one(seq: int, url: str) -> bool:
769788 url ,
770789 data = image_bytes , # Binary image data
771790 headers = {
772- "Content-Type" : f"image/ { format_str } " ,
791+ "Content-Type" : content_type ,
773792 },
774793 timeout = 30 , # 30 second timeout per screenshot
775794 )
0 commit comments