import requests import json import base64 import os # API_URL = "http://192.168.0.150:7000/translate_image" API_URL = "http://127.0.0.1:7000/translate_image" # 이미지 파일을 base64로 변환하는 함수 def image_to_base64(image_path): """이미지 파일을 base64 문자열로 변환""" if not os.path.exists(image_path): raise FileNotFoundError(f"이미지 파일을 찾을 수 없습니다: {image_path}") with open(image_path, "rb") as image_file: encoded_string = base64.b64encode(image_file.read()).decode('utf-8') return encoded_string # base64 데이터를 이미지 파일로 저장하는 함수 def base64_to_image(base64_data, output_path): """base64 문자열을 이미지 파일로 저장""" try: image_data = base64.b64decode(base64_data) with open(output_path, "wb") as image_file: image_file.write(image_data) return True except Exception as e: print(f"이미지 저장 중 오류: {e}") return False # 이미지 파일 경로 image_path = "d:/py/IT_Server/modules/img/6.jpg" # 이미지를 base64로 변환 try: image_base64 = image_to_base64(image_path) print(f"이미지 파일 '{image_path}' 를 base64로 변환했습니다.") print(f"Base64 길이: {len(image_base64)} 문자") except FileNotFoundError as e: print(f"오류: {e}") exit(1) payload = { "image_data": image_base64, # 필드명을 image_data로 수정 "file_prefix": "test", "toggle_states": {"ocr": True}, "unwanted_texts": {"크리스탈": "크리미"}, "watermark_text": "테스트 워터마크", "watermark_opacity": 0.5, "watermark_font_size": 32 } headers = {"Content-Type": "application/json"} print("API 요청을 보내는 중...") response = requests.post(API_URL, data=json.dumps(payload), headers=headers) print("응답 결과:") print(f"상태 코드: {response.status_code}") try: response_data = response.json() print("응답 내용:") if "error" in response_data: print(f"오류: {response_data['error']}") elif "result" in response_data and response_data.get("format") == "base64": result_base64 = response_data["result"] print(f"처리된 이미지 base64 길이: {len(result_base64)} 문자") # 결과 이미지를 파일로 저장 output_path = "d:/py/IT_Server/modules/translated_result.png" if base64_to_image(result_base64, output_path): print(f"처리된 이미지가 저장되었습니다: {output_path}") else: print("이미지 저장에 실패했습니다.") else: print("예상치 못한 응답 형식:") print(json.dumps(response_data, indent=2, ensure_ascii=False)) except json.JSONDecodeError: print("JSON 응답이 아닙니다:") print(response.text)