File size: 2,413 Bytes
a9d2861 2d99697 57ddb95 2d99697 e253bbe 57ddb95 e253bbe 57ddb95 e253bbe 57ddb95 e253bbe 57ddb95 e253bbe 57ddb95 e253bbe 57ddb95 e253bbe 57ddb95 e253bbe 2d99697 e253bbe 57ddb95 dd8f65f e253bbe | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | 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()
|