example1 / app.py
Sangwon78's picture
Update app.py
e253bbe verified
Raw History Blame Contribute Delete
2.41 kB
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()