@@ -550,6 +550,10 @@ async def call_claude_3_with_ocr(messages, objective, model):
550
550
with open (screenshot_filename , "rb" ) as img_file :
551
551
img = Image .open (img_file )
552
552
553
+ # Convert RGBA to RGB
554
+ if img .mode == "RGBA" :
555
+ img = img .convert ("RGB" )
556
+
553
557
# Calculate the new dimensions while maintaining the aspect ratio
554
558
original_width , original_height = img .size
555
559
aspect_ratio = original_width / original_height
@@ -561,9 +565,11 @@ async def call_claude_3_with_ocr(messages, objective, model):
561
565
# Resize the image
562
566
img_resized = img .resize ((new_width , new_height ), Image .Resampling .LANCZOS )
563
567
564
- # Save the resized image to a BytesIO object
568
+ # Save the resized and converted image to a BytesIO object for JPEG format
565
569
img_buffer = io .BytesIO ()
566
- img_resized .save (img_buffer , format = "PNG" )
570
+ img_resized .save (
571
+ img_buffer , format = "JPEG" , quality = 85
572
+ ) # Adjust the quality parameter as needed
567
573
img_buffer .seek (0 )
568
574
569
575
# Encode the resized image as base64
@@ -581,7 +587,7 @@ async def call_claude_3_with_ocr(messages, objective, model):
581
587
"type" : "image" ,
582
588
"source" : {
583
589
"type" : "base64" ,
584
- "media_type" : "image/png " ,
590
+ "media_type" : "image/jpeg " ,
585
591
"data" : img_data ,
586
592
},
587
593
},
0 commit comments