import gradio as gr import cv2 import numpy as np import os def convert_to_grayscale(image): # 이미지를 흑백으로 변환 gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) return gray_image def convert_and_save(image): # 이미지를 흑백으로 변환하고, 다운로드 가능한 PNG 파일로 저장 gray_image = convert_to_grayscale(image) output_path = "output.png" cv2.imwrite(output_path, gray_image) return gray_image, output_path # 예시 이미지 파일 이름 example_files = ["1..png", "2..jpg", "3.png"] # 기본 예시 이미지 생성 함수 def create_example_image(file_path, text): # 기본 흰색 이미지에 텍스트 추가 image = np.ones((256, 256, 3), dtype=np.uint8) * 255 # 흰색 배경 cv2.putText( image, text, (50, 130), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2, cv2.LINE_AA ) cv2.imwrite(file_path, image) # 예시 이미지 준비 for i, file_name in enumerate(example_files, start=1): if not os.path.exists(file_name): print(f"{file_name} 파일이 존재하지 않습니다. 기본 이미지를 생성합니다.") create_example_image(file_name, f"Example {i}") # Gradio 인터페이스 정의 example_paths = [os.path.abspath(file) for file in example_files] iface = gr.Interface( fn=convert_and_save, inputs="image", outputs=["image", "file"], title="이미지 흑백 변환기", description="아래 예시 파일을 클릭하거나 직접 이미지를 업로드하세요.", examples=example_paths # 예시 이미지 경로 전달 ) if __name__ == "__main__": iface.launch()