Update app.py
Browse files
app.py
CHANGED
|
@@ -3,58 +3,56 @@ import cv2
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image, ImageEnhance
|
| 5 |
|
| 6 |
-
def process_image(image, filter_type, brightness, tone, sensitivity
|
| 7 |
-
# Convert
|
| 8 |
img_array = np.array(image)
|
| 9 |
if filter_type == "Grayscale":
|
| 10 |
-
|
| 11 |
-
|
| 12 |
elif filter_type == "Color":
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
# Apply tone and sensitivity filters (basic simulation)
|
| 16 |
-
processed_img = cv2.convertScaleAbs(processed_img, alpha=tone, beta=sensitivity)
|
| 17 |
|
|
|
|
|
|
|
|
|
|
| 18 |
# Convert to PIL for brightness adjustment
|
| 19 |
-
|
| 20 |
-
enhancer = ImageEnhance.Brightness(
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
processed_img = process_image(image, filter_type, brightness, tone, sensitivity, output_format)
|
| 38 |
-
return Image.fromarray(processed_img)
|
| 39 |
-
|
| 40 |
-
with gr.Blocks() as app:
|
| 41 |
gr.Markdown("### ์ด๋ฏธ์ง ๋ณํ ๋ฐ ๋ค์ด๋ก๋ ํ๋ก๊ทธ๋จ")
|
| 42 |
|
| 43 |
with gr.Row():
|
| 44 |
with gr.Column():
|
| 45 |
-
|
| 46 |
-
|
| 47 |
brightness = gr.Slider(0.5, 2.0, step=0.1, value=1.0, label="๋ฐ๊ธฐ ์กฐ์ ")
|
| 48 |
tone = gr.Slider(0.5, 2.0, step=0.1, value=1.0, label="ํค ์กฐ์ ")
|
| 49 |
-
sensitivity = gr.Slider(
|
| 50 |
-
output_format = gr.Radio(["JPG", "PNG", "PDF"], label="์ถ๋ ฅ ํ์ ์ ํ")
|
| 51 |
with gr.Column():
|
| 52 |
-
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
| 58 |
)
|
| 59 |
|
| 60 |
-
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image, ImageEnhance
|
| 5 |
|
| 6 |
+
def process_image(image, filter_type, brightness, tone, sensitivity):
|
| 7 |
+
# Convert image to OpenCV format
|
| 8 |
img_array = np.array(image)
|
| 9 |
if filter_type == "Grayscale":
|
| 10 |
+
img_processed = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
|
| 11 |
+
img_processed = cv2.cvtColor(img_processed, cv2.COLOR_GRAY2RGB) # Convert back to 3-channel
|
| 12 |
elif filter_type == "Color":
|
| 13 |
+
img_processed = img_array # Keep original color
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Adjust tone and sensitivity
|
| 16 |
+
img_processed = cv2.convertScaleAbs(img_processed, alpha=tone, beta=sensitivity)
|
| 17 |
+
|
| 18 |
# Convert to PIL for brightness adjustment
|
| 19 |
+
img_pil = Image.fromarray(img_processed)
|
| 20 |
+
enhancer = ImageEnhance.Brightness(img_pil)
|
| 21 |
+
img_brightened = enhancer.enhance(brightness)
|
| 22 |
+
|
| 23 |
+
return np.array(img_brightened)
|
| 24 |
+
|
| 25 |
+
def save_image(image, output_format):
|
| 26 |
+
output_path = f"output.{output_format.lower()}"
|
| 27 |
+
pil_image = Image.fromarray(image)
|
| 28 |
+
pil_image.save(output_path)
|
| 29 |
+
return output_path
|
| 30 |
+
|
| 31 |
+
def app_function(image, filter_type, brightness, tone, sensitivity, output_format):
|
| 32 |
+
processed_image = process_image(image, filter_type, brightness, tone, sensitivity)
|
| 33 |
+
download_path = save_image(processed_image, output_format)
|
| 34 |
+
return processed_image, download_path
|
| 35 |
+
|
| 36 |
+
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
gr.Markdown("### ์ด๋ฏธ์ง ๋ณํ ๋ฐ ๋ค์ด๋ก๋ ํ๋ก๊ทธ๋จ")
|
| 38 |
|
| 39 |
with gr.Row():
|
| 40 |
with gr.Column():
|
| 41 |
+
image_input = gr.Image(type="pil", label="์ด๋ฏธ์ง ์
๋ก๋")
|
| 42 |
+
filter_type = gr.Radio(["Grayscale", "Color"], label="ํํฐ ์ ํ", value="Grayscale")
|
| 43 |
brightness = gr.Slider(0.5, 2.0, step=0.1, value=1.0, label="๋ฐ๊ธฐ ์กฐ์ ")
|
| 44 |
tone = gr.Slider(0.5, 2.0, step=0.1, value=1.0, label="ํค ์กฐ์ ")
|
| 45 |
+
sensitivity = gr.Slider(-100, 100, step=10, value=0, label="ํํฐ ๊ฐ๋")
|
| 46 |
+
output_format = gr.Radio(["JPG", "PNG", "PDF"], label="์ถ๋ ฅ ํ์ ์ ํ", value="JPG")
|
| 47 |
with gr.Column():
|
| 48 |
+
output_image = gr.Image(label="๊ฒฐ๊ณผ ์ด๋ฏธ์ง")
|
| 49 |
+
download_button = gr.File(label="๋ค์ด๋ก๋")
|
| 50 |
|
| 51 |
+
btn = gr.Button("์ด๋ฏธ์ง ๋ณํ ๋ฐ ์ ์ฅ")
|
| 52 |
+
btn.click(
|
| 53 |
+
app_function,
|
| 54 |
+
inputs=[image_input, filter_type, brightness, tone, sensitivity, output_format],
|
| 55 |
+
outputs=[output_image, download_button]
|
| 56 |
)
|
| 57 |
|
| 58 |
+
demo.launch()
|