Skip to content

Commit ed1cd53

Browse files
committed
Fix call_claude_3_with_ocr screenshot too large issue
1 parent 608560a commit ed1cd53

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

operate/models/apis.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,10 @@ async def call_claude_3_with_ocr(messages, objective, model):
550550
with open(screenshot_filename, "rb") as img_file:
551551
img = Image.open(img_file)
552552

553+
# Convert RGBA to RGB
554+
if img.mode == "RGBA":
555+
img = img.convert("RGB")
556+
553557
# Calculate the new dimensions while maintaining the aspect ratio
554558
original_width, original_height = img.size
555559
aspect_ratio = original_width / original_height
@@ -561,9 +565,11 @@ async def call_claude_3_with_ocr(messages, objective, model):
561565
# Resize the image
562566
img_resized = img.resize((new_width, new_height), Image.Resampling.LANCZOS)
563567

564-
# Save the resized image to a BytesIO object
568+
# Save the resized and converted image to a BytesIO object for JPEG format
565569
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
567573
img_buffer.seek(0)
568574

569575
# Encode the resized image as base64
@@ -581,7 +587,7 @@ async def call_claude_3_with_ocr(messages, objective, model):
581587
"type": "image",
582588
"source": {
583589
"type": "base64",
584-
"media_type": "image/png",
590+
"media_type": "image/jpeg",
585591
"data": img_data,
586592
},
587593
},

0 commit comments

Comments
 (0)