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, grayscale): if grayscale: image = convert_to_grayscale(image) if filter_type == "Soft Glow": # Soft Glow 효과 gaussian = cv2.GaussianBlur(image, (15, 15), 0) soft_glow = cv2.addWeighted(image, 0.5, gaussian, 0.5, 0) return soft_glow elif filter_type == "Portrait Enhancer": # 간단한 포트레이트 향상 (예: 대비 및 밝기 조정) enhanced = cv2.detailEnhance(image, sigma_s=10, sigma_r=0.15) return enhanced elif filter_type == "Warm Tone": # 따뜻한 톤 효과 warm_image = cv2.applyColorMap(image, cv2.COLORMAP_AUTUMN) return warm_image elif filter_type == "Cold Tone": # 차가운 톤 효과 cold_image = cv2.applyColorMap(image, cv2.COLORMAP_WINTER) return cold_image elif filter_type == "High-Key": # High-Key 효과 (밝기 증가) high_key = cv2.convertScaleAbs(image, alpha=1.2, beta=30) return high_key elif filter_type == "Low-Key": # Low-Key 효과 (밝기 감소) low_key = cv2.convertScaleAbs(image, alpha=0.7, beta=-30) return low_key elif filter_type == "Haze": # Haze 효과 haze = cv2.addWeighted(image, 0.7, np.full(image.shape, 255, dtype=np.uint8), 0.3, 0) return haze else: # 필터를 적용하지 않음 return image def convert_and_save(image, filter_type, grayscale): # 선택한 필터와 흑백 변환 여부를 이미지에 적용 filtered_image = apply_filter(image, filter_type, grayscale) # 이미지를 저장 output_path = "output.jpg" cv2.imwrite(output_path, filtered_image) return filtered_image, output_path # Gradio 인터페이스 정의 iface = gr.Interface( fn=convert_and_save, inputs=[ "image", gr.Radio(["None", "Soft Glow", "Portrait Enhancer", "Warm Tone", "Cold Tone", "High-Key", "Low-Key", "Haze"], label="필터 선택"), gr.Checkbox(label="흑백 변환") ], outputs=["image", "file"], title="이미지 필터 및 흑백 변환기", description="이미지를 업로드하고 필터와 흑백 변환 여부를 선택하면, 변환된 이미지를 JPG 파일로 다운로드할 수 있습니다." ) iface.launch()