edit : imagen -> gemini로 변경, bingimg -> 사용불가 , 벡터검색api 이미지저장이아닌 데이터 전송하는 api 추가 , vactor -> vector 오타 수정
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
import os
|
||||
import base64
|
||||
|
||||
from const import ILLEGAL_FILE_NAME
|
||||
|
||||
|
||||
def prompt_to_filenames(prompt):
|
||||
"""
|
||||
prompt 에 사용할 수 없는 문자가 있으면 '_' 로 치환
|
||||
@@ -22,4 +26,36 @@ def download_range(download_count:int,max=4):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
|
||||
def image_to_base64_string(image_path: str) -> str:
|
||||
"""
|
||||
이미지 파일 경로를 입력받아 해당 이미지를 Base64 문자열로 인코딩하여 반환합니다.
|
||||
|
||||
Args:
|
||||
image_path (str): Base64로 변환할 이미지 파일의 경로 (예: "path/to/your/image.png").
|
||||
|
||||
Returns:
|
||||
str: 인코딩된 Base64 문자열. 이미지 파일을 찾을 수 없거나 읽을 수 없는 경우 빈 문자열을 반환합니다.
|
||||
|
||||
Raises:
|
||||
FileNotFoundError: 지정된 이미지 경로에 파일이 존재하지 않을 때 발생합니다.
|
||||
IOError: 파일을 읽는 도중 오류가 발생할 때 발생합니다.
|
||||
"""
|
||||
if not os.path.exists(image_path):
|
||||
raise FileNotFoundError(f"Image file not found at: {image_path}")
|
||||
|
||||
try:
|
||||
with open(image_path, "rb") as image_file:
|
||||
# 이미지 파일을 이진(binary) 모드로 읽습니다.
|
||||
binary_data = image_file.read()
|
||||
# 이진 데이터를 Base64로 인코딩합니다.
|
||||
base64_encoded_data = base64.b64encode(binary_data)
|
||||
# 바이트 문자열을 UTF-8 디코딩하여 일반 문자열로 반환합니다.
|
||||
base64_string = base64_encoded_data.decode('utf-8')
|
||||
return base64_string
|
||||
except IOError as e:
|
||||
raise IOError(f"Error reading image file {image_path}: {e}")
|
||||
except Exception as e:
|
||||
# 그 외 예외 처리
|
||||
raise Exception(f"An unexpected error occurred: {e}")
|
||||
Reference in New Issue
Block a user