import gradio as gr from PIL import Image import cv2 import numpy as np # 필터 적용 함수 def apply_filter(image, filter_type, intensity): img = np.array(image) if filter_type == "Soft Glow": gaussian = cv2.GaussianBlur(img, (15, 15), 0) img = cv2.addWeighted(img, 1 - intensity, gaussian, intensity, 0) elif filter_type == "Portrait Enhancer": img = cv2.bilateralFilter(img, 9, 75, 75) elif filter_type == "Warm Tone": img[:, :, 0] = cv2.add(img[:, :, 0], int(50 * intensity)) # Red 증가 elif filter_type == "Cold Tone": img[:, :, 2] = cv2.add(img[:, :, 2], int(50 * intensity)) # Blue 증가 elif filter_type == "High-Key": img = cv2.convertScaleAbs(img, alpha=1 + intensity, beta=int(30 * intensity)) elif filter_type == "Low-Key": img = cv2.convertScaleAbs(img, alpha=1 - intensity, beta=int(-30 * intensity)) elif filter_type == "Haze": img = cv2.addWeighted(img, 1 - intensity, np.full(img.shape, 255, dtype=np.uint8), intensity, 0) elif filter_type == "Monochrome": gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR) return Image.fromarray(img) # 파일 저장 함수 def save_file(image, format): # 'JPG'를 'JPEG'로 변환 format = "JPEG" if format.upper() == "JPG" else format.upper() file_path = f"processed_image.{format.lower()}" image.save(file_path, format=format) return file_path # 이미지 변환 후 반환 def process_image(image, filter_type, intensity, file_format): filtered_image = apply_filter(image, filter_type, intensity) download_path = save_file(filtered_image, file_format) return filtered_image, download_path # Gradio UI 구성 with gr.Blocks() as demo: gr.Markdown("# 📷 이미지 필터 변환기") gr.Markdown("이미지를 업로드하고 원하는 필터를 클릭해 변환해 보세요!") with gr.Row(): image_input = gr.Image(type="pil", label="이미지 선택") with gr.Column(): filter_selector = gr.Dropdown( ["Soft Glow", "Portrait Enhancer", "Warm Tone", "Cold Tone", "High-Key", "Low-Key", "Haze", "Monochrome"], label="필터 선택" ) intensity_slider = gr.Slider(0.0, 1.0, value=0.5, label="필터 강도") file_format_selector = gr.Radio(["JPG", "PNG", "PDF"], label="파일 형식") submit_button = gr.Button("변환 적용") output_image = gr.Image(label="변환된 이미지") download_button = gr.File(label="다운로드 링크") # 버튼 클릭 시 동작 연결 submit_button.click( process_image, inputs=[image_input, filter_selector, intensity_slider, file_format_selector], outputs=[output_image, download_button] ) # 실행 if __name__ == "__main__": demo.launch()