|
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() |
|
|