import gradio as gr import cv2 import numpy as np from PIL import Image, ImageEnhance def process_image(image, filter_type, brightness, tone, sensitivity): # Convert image to OpenCV format img_array = np.array(image) if filter_type == "Grayscale": img_processed = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY) img_processed = cv2.cvtColor(img_processed, cv2.COLOR_GRAY2RGB) # Convert back to 3-channel elif filter_type == "Color": img_processed = img_array # Keep original color # Adjust tone and sensitivity img_processed = cv2.convertScaleAbs(img_processed, alpha=tone, beta=sensitivity) # Convert to PIL for brightness adjustment img_pil = Image.fromarray(img_processed) enhancer = ImageEnhance.Brightness(img_pil) img_brightened = enhancer.enhance(brightness) return np.array(img_brightened) def save_image(image, output_format): output_path = f"output.{output_format.lower()}" pil_image = Image.fromarray(image) pil_image.save(output_path) return output_path def app_function(image, filter_type, brightness, tone, sensitivity, output_format): processed_image = process_image(image, filter_type, brightness, tone, sensitivity) download_path = save_image(processed_image, output_format) return processed_image, download_path with gr.Blocks() as demo: gr.Markdown("### 이미지 변환 및 다운로드 프로그램") with gr.Row(): with gr.Column(): image_input = gr.Image(type="pil", label="이미지 업로드") filter_type = gr.Radio(["Grayscale", "Color"], label="필터 선택", value="Grayscale") brightness = gr.Slider(0.5, 2.0, step=0.1, value=1.0, label="밝기 조절") tone = gr.Slider(0.5, 2.0, step=0.1, value=1.0, label="톤 조절") sensitivity = gr.Slider(-100, 100, step=10, value=0, label="필터 감도") output_format = gr.Radio(["JPG", "PNG", "PDF"], label="출력 형식 선택", value="JPG") with gr.Column(): output_image = gr.Image(label="결과 이미지") download_button = gr.File(label="다운로드") btn = gr.Button("이미지 변환 및 저장") btn.click( app_function, inputs=[image_input, filter_type, brightness, tone, sensitivity, output_format], outputs=[output_image, download_button] ) demo.launch()