import gradio as gr from PIL import Image, ImageEnhance, ImageOps, ImageFilter import numpy as np def apply_filter(image, filter_name, intensity): if filter_name == "Soft Glow": glow = image.filter(ImageFilter.GaussianBlur(intensity)) enhanced_image = Image.blend(image, glow, 0.5) return enhanced_image elif filter_name == "Portrait Enhancer": enhancer = ImageEnhance.Sharpness(image) image = enhancer.enhance(1 + 0.1 * intensity) enhancer = ImageEnhance.Color(image) image = enhancer.enhance(1 + 0.1 * intensity) return image elif filter_name == "Warm Tone": warm_tone = ImageEnhance.Color(image).enhance(1 + 0.1 * intensity) return warm_tone elif filter_name == "Cold Tone": blue_overlay = Image.new('RGB', image.size, (173, 216, 230)) blended_image = Image.blend(image, blue_overlay, 0.05 * intensity) enhancer = ImageEnhance.Sharpness(blended_image) sharpened_image = enhancer.enhance(1 + 0.3 * intensity) return sharpened_image elif filter_name == "High-Key": enhancer = ImageEnhance.Brightness(image) high_key = enhancer.enhance(1 + 0.1 * intensity) enhancer = ImageEnhance.Contrast(high_key) high_key = enhancer.enhance(0.8 + 0.02 * intensity) return high_key elif filter_name == "Low-Key": enhancer = ImageEnhance.Contrast(image) low_key = enhancer.enhance(1 + 0.1 * intensity) enhancer = ImageEnhance.Brightness(low_key) low_key = enhancer.enhance(1 - 0.05 * intensity) return low_key elif filter_name == "Haze": haze = image.filter(ImageFilter.GaussianBlur(2 * intensity)) enhancer = ImageEnhance.Brightness(haze) haze = enhancer.enhance(1 + 0.05 * intensity) return haze elif filter_name == "Monochrome": mono = image.convert("L") mono = ImageOps.colorize(mono, black="black", white="white") enhancer = ImageEnhance.Contrast(mono) mono = enhancer.enhance(1 + 0.1 * intensity) return mono return image def convert_to_grayscale(image): gray_image = ImageOps.grayscale(image) return gray_image def convert_and_save(image, filter_type, intensity): filtered_image = apply_filter(image, filter_type, intensity) output_path = "output.jpg" filtered_image.save(output_path) return filtered_image, output_path iface = gr.Interface( fn=convert_and_save, inputs=[ gr.Image(type="pil", label="이미지 업로드"), gr.Radio( ["Grayscale", "Soft Glow", "Portrait Enhancer", "Warm Tone", "Cold Tone", "High-Key", "Low-Key", "Haze", "Monochrome"], label="필터 선택" ), gr.Slider(minimum=1, maximum=100, value=50, label="필터 강도") ], outputs=["image", "file"], title="이미지 필터 및 흑백 변환기", description="이미지를 업로드하고 필터와 강도를 선택하면, 변환된 이미지를 JPG 파일로 다운로드할 수 있습니다." ) iface.launch()