Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,60 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import io
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
return
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
with gr.Row():
|
| 38 |
-
input_image = gr.Image(type="pil", label="์ด๋ฏธ์ง ์
๋ก๋")
|
| 39 |
-
file_format = gr.Radio(["JPG", "PNG", "PDF"], value="JPG", label="ํ์ผ ํ์ ์ ํ")
|
| 40 |
-
|
| 41 |
-
with gr.Row():
|
| 42 |
-
brightness = gr.Slider(0.5, 2.0, value=1.0, step=0.1, label="๋ฐ๊ธฐ")
|
| 43 |
-
contrast = gr.Slider(0.5, 2.0, value=1.0, step=0.1, label="๋๋น")
|
| 44 |
-
tone = gr.Slider(-50, 50, value=0, step=1, label="ํค")
|
| 45 |
-
|
| 46 |
-
with gr.Row():
|
| 47 |
-
gray_output = gr.Image(label="ํ๋ฐฑ ์ด๋ฏธ์ง")
|
| 48 |
-
color_output = gr.Image(label="์ปฌ๋ฌ ์ด๋ฏธ์ง")
|
| 49 |
-
|
| 50 |
-
with gr.Row():
|
| 51 |
-
gray_download = gr.File(label="ํ๋ฐฑ ์ด๋ฏธ์ง ๋ค์ด๋ก๋")
|
| 52 |
-
color_download = gr.File(label="์ปฌ๋ฌ ์ด๋ฏธ์ง ๋ค์ด๋ก๋")
|
| 53 |
-
|
| 54 |
-
def update(image, brightness, contrast, tone, file_format):
|
| 55 |
-
gray_img, color_img = process_image(image, brightness, contrast, tone)
|
| 56 |
-
gray_file = download_image(gray_img, file_format)
|
| 57 |
-
color_file = download_image(color_img, file_format)
|
| 58 |
-
return gray_img, color_img, gray_file, color_file
|
| 59 |
|
| 60 |
-
input_image.change(
|
| 61 |
-
update,
|
| 62 |
-
inputs=[input_image, brightness, contrast, tone, file_format],
|
| 63 |
-
outputs=[gray_output, color_output, gray_download, color_download]
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
app.launch()
|
| 67 |
-
|
| 68 |
-
if __name__ == "__main__":
|
| 69 |
-
main()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
+
from PIL import Image, ImageEnhance
|
|
|
|
| 5 |
|
| 6 |
+
def process_image(image, filter_type, brightness, tone, sensitivity, output_format):
|
| 7 |
+
# Convert uploaded image to OpenCV format
|
| 8 |
+
img_array = np.array(image)
|
| 9 |
+
if filter_type == "Grayscale":
|
| 10 |
+
processed_img = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
|
| 11 |
+
processed_img = cv2.cvtColor(processed_img, cv2.COLOR_GRAY2RGB) # Grayscale ์ ์ง ํ RGB๋ก ๋ณํ
|
| 12 |
+
elif filter_type == "Color":
|
| 13 |
+
processed_img = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR) # OpenCV์ ๊ธฐ๋ณธ BGR ์ฌ์ฉ
|
| 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 |
+
processed_img = Image.fromarray(processed_img)
|
| 20 |
+
enhancer = ImageEnhance.Brightness(processed_img)
|
| 21 |
+
processed_img = enhancer.enhance(brightness)
|
| 22 |
+
|
| 23 |
+
# Convert final image for display
|
| 24 |
+
final_img = np.array(processed_img)
|
| 25 |
+
return final_img
|
| 26 |
|
| 27 |
+
# Gradio Interface
|
| 28 |
+
def download_image(image, output_format):
|
| 29 |
+
if output_format == "JPG":
|
| 30 |
+
return image.save("output.jpg", "JPEG")
|
| 31 |
+
elif output_format == "PNG":
|
| 32 |
+
return image.save("output.png", "PNG")
|
| 33 |
+
elif output_format == "PDF":
|
| 34 |
+
return image.save("output.pdf", "PDF")
|
| 35 |
|
| 36 |
+
def app_ui(image, filter_type, brightness, tone, sensitivity, output_format):
|
| 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 |
+
img_input = gr.Image(label="์ด๋ฏธ์ง ์
๋ก๋", type="pil")
|
| 46 |
+
filter_choice = gr.Radio(["Grayscale", "Color"], label="ํํฐ ์ ํ")
|
| 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(0, 100, step=10, value=0, label="ํํฐ ๊ฐ๋")
|
| 50 |
+
output_format = gr.Radio(["JPG", "PNG", "PDF"], label="์ถ๋ ฅ ํ์ ์ ํ")
|
| 51 |
+
with gr.Column():
|
| 52 |
+
img_output = gr.Image(label="๊ฒฐ๊ณผ ์ด๋ฏธ์ง")
|
| 53 |
|
| 54 |
+
img_output = gr.Interface(
|
| 55 |
+
fn=app_ui,
|
| 56 |
+
inputs=[img_input, filter_choice, brightness, tone, sensitivity, output_format],
|
| 57 |
+
outputs=img_output,
|
| 58 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
app.launch()
|
|
|
|
|
|
|
|
|