From 1c5ccf4b1ca325b5f99ca8a9e6872114ec4f7a84 Mon Sep 17 00:00:00 2001 From: Johannes Date: Wed, 8 Apr 2026 14:11:16 +0200 Subject: [PATCH] mvp --- .gitignore | 7 ++++ image_query.py | 86 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 2 ++ 3 files changed, 95 insertions(+) create mode 100644 .gitignore create mode 100644 image_query.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..21a1491 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.env +__pycache__/ +*.pyc +*.pyo +response.txt +out.txt +Test_pic.jpg \ No newline at end of file diff --git a/image_query.py b/image_query.py new file mode 100644 index 0000000..e029c54 --- /dev/null +++ b/image_query.py @@ -0,0 +1,86 @@ +import anthropic +import base64 +import sys +from pathlib import Path +from dotenv import load_dotenv + +load_dotenv() + +MEDIA_TYPES = { + ".jpg": "image/jpeg", + ".jpeg": "image/jpeg", + ".png": "image/png", + ".gif": "image/gif", + ".webp": "image/webp", +} + + +def get_media_type(image_path: str) -> str: + ext = Path(image_path).suffix.lower() + media_type = MEDIA_TYPES.get(ext) + if not media_type: + raise ValueError(f"Unsupported image format: {ext}. Use jpg, png, gif, or webp.") + return media_type + + +def load_image_b64(image_path: str) -> str: + with open(image_path, "rb") as f: + return base64.standard_b64encode(f.read()).decode("utf-8") + + +def query_claude(image_path: str, prompt: str) -> str: + client = anthropic.Anthropic() + media_type = get_media_type(image_path) + image_data = load_image_b64(image_path) + + response = client.messages.create( + model="claude-opus-4-6", + max_tokens=4096, + messages=[ + { + "role": "user", + "content": [ + { + "type": "image", + "source": { + "type": "base64", + "media_type": media_type, + "data": image_data, + }, + }, + {"type": "text", "text": prompt}, + ], + } + ], + ) + + return next(block.text for block in response.content if block.type == "text") + + +def main(): + if len(sys.argv) < 3: + print("Usage: python image_query.py \"\" [output_file]") + sys.exit(1) + + image_path = sys.argv[1] + prompt = sys.argv[2] + output_file = sys.argv[3] if len(sys.argv) > 3 else "response.txt" + + if not Path(image_path).exists(): + print(f"Error: image file '{image_path}' not found.") + sys.exit(1) + + print(f"Querying Claude about '{image_path}'...") + response_text = query_claude(image_path, prompt) + + print("\n--- Response ---") + print(response_text) + + with open(output_file, "w", encoding="utf-8") as f: + f.write(response_text) + + print(f"\nSaved to {output_file}") + + +if __name__ == "__main__": + main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..0e59774 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +anthropic +python-dotenv