Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)
Python
Move anthropic image processing
Abi Rajab5c2ef9d9d7e
diff --git a/backend/image_processing/utils.py b/backend/agent/providers/anthropic/image_processing.pysimilarity index 73%rename from backend/image_processing/utils.pyrename to backend/agent/providers/anthropic/image_processing.pyindex fcd22c44..99fc584c 100644--- a/backend/image_processing/utils.py+++ b/backend/agent/providers/anthropic/image_processing.py@@ -1,41 +1,34 @@import base64import ioimport time+from PIL import Image+CLAUDE_IMAGE_MAX_SIZE = 5 * 1024 * 1024CLAUDE_MAX_IMAGE_DIMENSION = 7990-# Process image so it meets Claude requirementsdef process_image(image_data_url: str) -> tuple[str, str]:-- # Extract bytes and media type from base64 data URLmedia_type = image_data_url.split(";")[0].split(":")[1]base64_data = image_data_url.split(",")[1]image_bytes = base64.b64decode(base64_data)img = Image.open(io.BytesIO(image_bytes))- # Check if image is under max dimensions and sizeis_under_dimension_limit = (img.width < CLAUDE_MAX_IMAGE_DIMENSIONand img.height < CLAUDE_MAX_IMAGE_DIMENSION)is_under_size_limit = len(base64_data) <= CLAUDE_IMAGE_MAX_SIZE- # If image is under both limits, no processing neededif is_under_dimension_limit and is_under_size_limit:print("[CLAUDE IMAGE PROCESSING] no processing needed")return (media_type, base64_data)- # Time image processingstart_time = time.time()- # Check if either dimension exceeds 7900px (Claude disallows >= 8000px)- # Resize image if neededif not is_under_dimension_limit:- # Calculate the new dimensions while maintaining aspect ratioif img.width > img.height:new_width = CLAUDE_MAX_IMAGE_DIMENSIONnew_height = int((CLAUDE_MAX_IMAGE_DIMENSION / img.width) * img.height)@@ -43,21 +36,16 @@ def process_image(image_data_url: str) -> tuple[str, str]:new_height = CLAUDE_MAX_IMAGE_DIMENSIONnew_width = int((CLAUDE_MAX_IMAGE_DIMENSION / img.height) * img.width)- # Resize the imageimg = img.resize((new_width, new_height), Image.DEFAULT_STRATEGY)print(f"[CLAUDE IMAGE PROCESSING] image resized: width = {new_width}, height = {new_height}")- # Convert and compress as JPEG- # We always compress as JPEG (95% at the least) even when we resize and the original image- # is under the size limit.quality = 95output = io.BytesIO()- img = img.convert("RGB") # Ensure image is in RGB mode for JPEG conversion+ img = img.convert("RGB")img.save(output, format="JPEG", quality=quality)- # Reduce quality until image is under max sizewhile (len(base64.b64encode(output.getvalue())) > CLAUDE_IMAGE_MAX_SIZEand quality > 10@@ -66,7 +54,6 @@ def process_image(image_data_url: str) -> tuple[str, str]:img.save(output, format="JPEG", quality=quality)quality -= 5- # Log so we know it was modifiedold_size = len(base64_data)new_size = len(base64.b64encode(output.getvalue()))print(diff --git a/backend/agent/providers/anthropic/transform.py b/backend/agent/providers/anthropic/transform.pyindex 8f91c4db..207f4d01 100644--- a/backend/agent/providers/anthropic/transform.py+++ b/backend/agent/providers/anthropic/transform.py@@ -2,8 +2,7 @@ import copyfrom typing import Any, Dict, List, Tuple, castfrom openai.types.chat import ChatCompletionMessageParam--from image_processing.utils import process_image+from agent.providers.anthropic.image_processing import process_imagedef convert_openai_messages_to_claude(diff --git a/backend/image_processing/__init__.py b/backend/image_processing/__init__.pydeleted file mode 100644index e69de29b..00000000