import gradio as gr import cv2 import numpy as np def convert_to_grayscale(image): # 이미지를 흑백으로 변환 gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) return gray_image def apply_filter(image, filter_type): if filter_type == "Soft Glow": # 부드러운 빛 효과 blurred = cv2.GaussianBlur(image, (15, 15), 0) soft_glow = cv2.addWeighted(image, 0.5, blurred, 0.5, 0) return soft_glow elif filter_type == "Portrait Enhancer": # 초상화 강화 효과 kernel = np.array([[0, -1, 0], [-1, 5,-1], [0, -1, 0]]) enhanced = cv2.filter2D(image, -1, kernel) return enhanced elif filter_type == "Warm Tone": # 따뜻한 톤 효과 warm = cv2.applyColorMap(image, cv2.COLORMAP_AUTUMN) return warm elif filter_type == "Cold Tone": # 차가운 톤 효과 cold = cv2.applyColorMap(image, cv2.COLORMAP_WINTER) return cold elif filter_type == "High-Key": # 하이키 효과 high_key = cv2.convertScaleAbs(image, alpha=1.2, beta=50) return high_key elif filter_type == "Low-Key": # 로우키 효과 low_key = cv2.convertScaleAbs(image, alpha=0.8, beta=-30) return low_key elif filter_type == "Haze": # 헤이즈 효과 haze = cv2.addWeighted(image, 0.8, np.full(image.shape, 255, dtype=np.uint8), 0.2, 0) return haze else: # 필터가 선택되지 않았을 경우 원본 반환 return image def convert_and_save_with_filter(image, filter_type): # 선택한 필터 적용 및 흑백 변환 filtered_image = apply_filter(image, filter_type) gray_image = convert_to_grayscale(filtered_image) output_path = "output.jpg" cv2.imwrite(output_path, gray_image) return gray_image, output_path # Gradio 인터페이스 정의 iface = gr.Interface( fn=convert_and_save_with_filter, inputs=["image", gr.Radio(["Soft Glow", "Portrait Enhancer", "Warm Tone", "Cold Tone", "High-Key", "Low-Key", "Haze"], label="Select Filter")], outputs=["image", "file"], title="이미지 흑백 변환기 및 필터 적용", description="이미지를 업로드하고 필터를 선택하면 흑백으로 변환하고 JPG 파일로 다운로드할 수 있습니다." ) if __name__ == "__main__": iface.launch()