Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,67 @@
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
| 3 |
-
import gradio as gr
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
gray_image = cv2.convertScaleAbs(gray_image, alpha=filter_intensity/100)
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
#
|
| 21 |
gray_pil = Image.fromarray(gray_image)
|
| 22 |
-
color_pil = Image.fromarray(
|
|
|
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
|
|
|
| 27 |
with gr.Blocks() as app:
|
| 28 |
-
gr.Markdown("# 이미지
|
| 29 |
-
|
| 30 |
with gr.Row():
|
| 31 |
image_input = gr.Image(type="pil", label="이미지 업로드")
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
with gr.Row():
|
| 36 |
gray_output = gr.Image(label="흑백 이미지")
|
| 37 |
color_output = gr.Image(label="컬러 이미지")
|
| 38 |
-
|
| 39 |
with gr.Row():
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
if __name__ == "__main__":
|
| 51 |
-
|
| 52 |
-
app.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
# 이미지 변환 함수
|
| 7 |
+
def process_image(image, brightness=1.0, contrast=1.0, tone=0.0):
|
| 8 |
+
# OpenCV로 이미지 변환
|
| 9 |
+
image = np.array(image)
|
| 10 |
+
gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
| 11 |
+
color_image = cv2.cvtColor(gray_image, cv2.COLOR_GRAY2BGR)
|
|
|
|
| 12 |
|
| 13 |
+
# 밝기 및 대비 조정
|
| 14 |
+
gray_image = cv2.convertScaleAbs(gray_image, alpha=contrast, beta=brightness * 50)
|
| 15 |
+
color_image = cv2.convertScaleAbs(color_image, alpha=contrast, beta=brightness * 50)
|
| 16 |
|
| 17 |
+
# 톤 조정 (HSV 색상 조정)
|
| 18 |
+
hsv_image = cv2.cvtColor(color_image, cv2.COLOR_BGR2HSV)
|
| 19 |
+
hsv_image[:, :, 0] = np.clip(hsv_image[:, :, 0] + tone, 0, 179)
|
| 20 |
+
color_image = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR)
|
| 21 |
|
| 22 |
+
# 이미지 반환
|
| 23 |
gray_pil = Image.fromarray(gray_image)
|
| 24 |
+
color_pil = Image.fromarray(color_image)
|
| 25 |
+
return gray_pil, color_pil
|
| 26 |
|
| 27 |
+
# 파일 저장 함수
|
| 28 |
+
def save_image(image, file_format):
|
| 29 |
+
image_io = io.BytesIO()
|
| 30 |
+
image.save(image_io, format=file_format.upper())
|
| 31 |
+
return image_io.getvalue()
|
| 32 |
|
| 33 |
+
# Gradio 인터페이스
|
| 34 |
+
def main():
|
| 35 |
with gr.Blocks() as app:
|
| 36 |
+
gr.Markdown("## 이미지 변환 및 조정 앱")
|
| 37 |
+
|
| 38 |
with gr.Row():
|
| 39 |
image_input = gr.Image(type="pil", label="이미지 업로드")
|
| 40 |
+
file_format = gr.Radio(["JPG", "PNG", "PDF"], value="JPG", label="다운로드 형식")
|
| 41 |
+
|
| 42 |
+
with gr.Row():
|
| 43 |
+
brightness = gr.Slider(0.5, 2.0, value=1.0, step=0.1, label="밝기 조정")
|
| 44 |
+
contrast = gr.Slider(0.5, 2.0, value=1.0, step=0.1, label="대비 조정")
|
| 45 |
+
tone = gr.Slider(-50, 50, value=0, step=1, label="톤 조정")
|
| 46 |
+
|
| 47 |
with gr.Row():
|
| 48 |
gray_output = gr.Image(label="흑백 이미지")
|
| 49 |
color_output = gr.Image(label="컬러 이미지")
|
| 50 |
+
|
| 51 |
with gr.Row():
|
| 52 |
+
download_gray = gr.File(label="흑백 이미지 다운로드")
|
| 53 |
+
download_color = gr.File(label="컬러 이미지 다운로드")
|
| 54 |
+
|
| 55 |
+
def update(image, brightness, contrast, tone, file_format):
|
| 56 |
+
gray_img, color_img = process_image(image, brightness, contrast, tone)
|
| 57 |
+
gray_bytes = save_image(gray_img, file_format)
|
| 58 |
+
color_bytes = save_image(color_img, file_format)
|
| 59 |
+
return gray_img, color_img, gray_bytes, color_bytes
|
| 60 |
|
| 61 |
+
image_input.change(update, [image_input, brightness, contrast, tone, file_format],
|
| 62 |
+
[gray_output, color_output, download_gray, download_color])
|
| 63 |
+
|
| 64 |
+
app.launch()
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
| 67 |
+
main()
|
|
|