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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
from PIL import Image, ImageEnhance, ImageFilter
|
4 |
+
import gradio as gr
|
5 |
+
from io import BytesIO
|
6 |
+
import tempfile
|
7 |
+
import logging
|
8 |
+
|
9 |
+
# 로깅 설정 - INFO 레벨로 변경
|
10 |
+
logging.basicConfig(level=logging.INFO)
|
11 |
+
logger = logging.getLogger(__name__)
|
12 |
+
|
13 |
+
def adjust_brightness(image, value):
|
14 |
+
"""이미지 밝기 조절"""
|
15 |
+
value = float(value - 1) * 100 # 0-2 범위를 -100에서 +100으로 변환
|
16 |
+
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
|
17 |
+
h, s, v = cv2.split(hsv)
|
18 |
+
v = cv2.add(v, value)
|
19 |
+
v = np.clip(v, 0, 255)
|
20 |
+
final_hsv = cv2.merge((h, s, v))
|
21 |
+
return cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)
|
22 |
+
|
23 |
+
def adjust_contrast(image, value):
|
24 |
+
"""이미지 대비 조절"""
|
25 |
+
value = float(value)
|
26 |
+
return np.clip(image * value, 0, 255).astype(np.uint8)
|
27 |
+
|
28 |
+
def adjust_saturation(image, value):
|
29 |
+
"""이미지 채도 조절"""
|
30 |
+
value = float(value - 1) * 100 # 0-2 범위를 -100에서 +100으로 변환
|
31 |
+
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
|
32 |
+
h, s, v = cv2.split(hsv)
|
33 |
+
s = cv2.add(s, value)
|
34 |
+
s = np.clip(s, 0, 255)
|
35 |
+
final_hsv = cv2.merge((h, s, v))
|
36 |
+
return cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)
|
37 |
+
|
38 |
+
def adjust_temperature(image, value):
|
39 |
+
"""이미지 색온도 조절 (색상 밸런스)"""
|
40 |
+
value = float(value) * 30 # 효과 스케일 조절
|
41 |
+
b, g, r = cv2.split(image)
|
42 |
+
if value > 0: # 따뜻하게
|
43 |
+
r = cv2.add(r, value)
|
44 |
+
b = cv2.subtract(b, value)
|
45 |
+
else: # 차갑게
|
46 |
+
r = cv2.add(r, value)
|
47 |
+
b = cv2.subtract(b, value)
|
48 |
+
|
49 |
+
r = np.clip(r, 0, 255)
|
50 |
+
b = np.clip(b, 0, 255)
|
51 |
+
return cv2.merge([b, g, r])
|
52 |
+
|
53 |
+
def adjust_tint(image, value):
|
54 |
+
"""이미지 색조 조절"""
|
55 |
+
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
|
56 |
+
h, s, v = cv2.split(hsv_image)
|
57 |
+
h = cv2.add(h, int(value))
|
58 |
+
h = np.clip(h, 0, 179) # Hue 값은 0-179 범위
|
59 |
+
final_hsv = cv2.merge((h, s, v))
|
60 |
+
return cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)
|
61 |
+
|
62 |
+
def adjust_exposure(image, value):
|
63 |
+
"""이미지 노출 조절"""
|
64 |
+
enhancer = ImageEnhance.Brightness(Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)))
|
65 |
+
img_enhanced = enhancer.enhance(1 + float(value) / 5.0)
|
66 |
+
return cv2.cvtColor(np.array(img_enhanced), cv2.COLOR_RGB2BGR)
|
67 |
+
|
68 |
+
def adjust_vibrance(image, value):
|
69 |
+
"""이미지 활기 조절"""
|
70 |
+
img = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
71 |
+
converter = ImageEnhance.Color(img)
|
72 |
+
factor = 1 + (float(value) / 100.0)
|
73 |
+
img = converter.enhance(factor)
|
74 |
+
return cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
|
75 |
+
|
76 |
+
def adjust_color_mixer_blues(image, value):
|
77 |
+
"""이미지 컬러 믹서 (블루) 조절"""
|
78 |
+
b, g, r = cv2.split(image)
|
79 |
+
b = cv2.add(b, float(value))
|
80 |
+
b = np.clip(b, 0, 255)
|
81 |
+
return cv2.merge([b, g, r])
|
82 |
+
|
83 |
+
def adjust_shadows(image, value):
|
84 |
+
"""이미지 그림자 조절"""
|
85 |
+
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
86 |
+
enhancer = ImageEnhance.Brightness(pil_image)
|
87 |
+
factor = 1 + (float(value) / 100.0)
|
88 |
+
pil_image = enhancer.enhance(factor)
|
89 |
+
return cv2.cvtColor(np.array(pil_image), cv2.COLOR_BGR2RGB)
|
90 |
+
|
91 |
+
def process_image(image, brightness, contrast, saturation, temperature, tint, exposure, vibrance, color_mixer_blues, shadows):
|
92 |
+
"""모든 조정 사항을 이미지에 적용"""
|
93 |
+
if image is None:
|
94 |
+
return None
|
95 |
+
|
96 |
+
# PIL 이미지를 OpenCV 형식으로 변환
|
97 |
+
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
98 |
+
|
99 |
+
# 조정 사항 순차 적용
|
100 |
+
image = adjust_brightness(image, brightness)
|
101 |
+
image = adjust_contrast(image, contrast)
|
102 |
+
image = adjust_saturation(image, saturation)
|
103 |
+
image = adjust_temperature(image, temperature)
|
104 |
+
image = adjust_tint(image, tint)
|
105 |
+
image = adjust_exposure(image, exposure)
|
106 |
+
image = adjust_vibrance(image, vibrance)
|
107 |
+
image = adjust_color_mixer_blues(image, color_mixer_blues)
|
108 |
+
image = adjust_shadows(image, shadows)
|
109 |
+
|
110 |
+
# PIL 이미지로 다시 변환
|
111 |
+
return Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
112 |
+
|
113 |
+
def download_image(image, input_image_name):
|
114 |
+
"""이미지를 JPG 형식으로 저장하고 경로 반환"""
|
115 |
+
if image is None:
|
116 |
+
return None
|
117 |
+
|
118 |
+
# 한국 시간 타임스탬프 생성 함수
|
119 |
+
from datetime import datetime, timedelta
|
120 |
+
def get_korean_timestamp():
|
121 |
+
korea_time = datetime.utcnow() + timedelta(hours=9)
|
122 |
+
return korea_time.strftime('%Y%m%d_%H%M%S')
|
123 |
+
|
124 |
+
timestamp = get_korean_timestamp()
|
125 |
+
if input_image_name and hasattr(input_image_name, 'name'):
|
126 |
+
base_name = input_image_name.name.split('.')[0] # 파일 객체에서 이름 추출
|
127 |
+
else:
|
128 |
+
base_name = "이미지"
|
129 |
+
|
130 |
+
file_name = f"[끝장AI]끝장필터_{base_name}_{timestamp}.jpg"
|
131 |
+
|
132 |
+
# 파일 저장
|
133 |
+
temp_file_path = tempfile.gettempdir() + "/" + file_name
|
134 |
+
image.save(temp_file_path, format="JPEG")
|
135 |
+
return temp_file_path
|
136 |
+
|
137 |
+
def create_interface():
|
138 |
+
css = """
|
139 |
+
footer {
|
140 |
+
visibility: hidden;
|
141 |
+
}
|
142 |
+
.download-button, .download-output {
|
143 |
+
width: 100%;
|
144 |
+
}
|
145 |
+
.download-container {
|
146 |
+
display: flex;
|
147 |
+
flex-direction: column;
|
148 |
+
align-items: center;
|
149 |
+
width: 100%;
|
150 |
+
}
|
151 |
+
#gradio-app {
|
152 |
+
margin: 0 !important; /* 모든 방향 여백 제거 */
|
153 |
+
text-align: left !important; /* 왼쪽 정렬 강제 */
|
154 |
+
padding: 20px !important; /* 패딩 추가 */
|
155 |
+
}
|
156 |
+
.gradio-container {
|
157 |
+
max-width: 100% !important; /* 가로 폭 전체 사용 */
|
158 |
+
margin-left: 0 !important; /* 왼쪽 정렬 */
|
159 |
+
padding: 20px !important; /* 패딩 추가 */
|
160 |
+
}
|
161 |
+
.download-button {
|
162 |
+
background-color: black !important;
|
163 |
+
color: white !important;
|
164 |
+
border: none !important;
|
165 |
+
padding: 10px !important;
|
166 |
+
font-size: 16px !important;
|
167 |
+
}
|
168 |
+
"""
|
169 |
+
|
170 |
+
with gr.Blocks(theme=gr.themes.Soft(
|
171 |
+
primary_hue=gr.themes.Color(
|
172 |
+
c50="#FFF7ED", # 가장 밝은 주황
|
173 |
+
c100="#FFEDD5",
|
174 |
+
c200="#FED7AA",
|
175 |
+
c300="#FDBA74",
|
176 |
+
c400="#FB923C",
|
177 |
+
c500="#F97316", # 기본 주황
|
178 |
+
c600="#EA580C",
|
179 |
+
c700="#C2410C",
|
180 |
+
c800="#9A3412",
|
181 |
+
c900="#7C2D12", # 가장 어두운 주황
|
182 |
+
c950="#431407",
|
183 |
+
),
|
184 |
+
secondary_hue="zinc", # 모던한 느낌의 회색 계열
|
185 |
+
neutral_hue="zinc",
|
186 |
+
font=("Pretendard", "sans-serif")
|
187 |
+
), css=css) as interface:
|
188 |
+
|
189 |
+
with gr.Row():
|
190 |
+
# 왼쪽 열: 비율 3
|
191 |
+
with gr.Column(scale=3):
|
192 |
+
# 이미지 업로드
|
193 |
+
input_image = gr.Image(type="pil", label="이미지 업로드")
|
194 |
+
|
195 |
+
# 조정 슬라이더
|
196 |
+
brightness_slider = gr.Slider(0.0, 2.0, value=1.0, step=0.1, label="밝기 조절")
|
197 |
+
contrast_slider = gr.Slider(0.5, 1.5, value=1.0, step=0.1, label="대비 조절")
|
198 |
+
saturation_slider = gr.Slider(0.0, 2.0, value=1.0, step=0.1, label="채도 조절")
|
199 |
+
temperature_slider = gr.Slider(-1.0, 1.0, value=0.0, step=0.1, label="색온도 조절")
|
200 |
+
tint_slider = gr.Slider(-100, 100, value=0, step=1, label="색조 조절")
|
201 |
+
exposure_slider = gr.Slider(-5.0, 5.0, value=0.0, step=0.1, label="노출 조절")
|
202 |
+
vibrance_slider = gr.Slider(-100.0, 100.0, value=0.0, step=1.0, label="활기 조절")
|
203 |
+
color_mixer_blues_slider = gr.Slider(-100.0, 100.0, value=0.0, step=1.0, label="컬러 믹서 (블루)")
|
204 |
+
shadows_slider = gr.Slider(-100.0, 100.0, value=0.0, step=1.0, label="그림자 조절")
|
205 |
+
|
206 |
+
# 오른쪽 열: 비율 7
|
207 |
+
with gr.Column(scale=7):
|
208 |
+
# 처리된 이미지 출력
|
209 |
+
output_image = gr.Image(type="pil", label="처리된 이미지")
|
210 |
+
|
211 |
+
# 변환된 이미지 다운로드 버튼
|
212 |
+
with gr.Row(elem_classes="download-container"):
|
213 |
+
download_button = gr.Button("JPG로 변환하기", elem_classes="download-button")
|
214 |
+
with gr.Row(elem_classes="download-container"):
|
215 |
+
download_output = gr.File(label="JPG 이미지 다운로드", elem_classes="download-output")
|
216 |
+
|
217 |
+
# 이미지 처리 함수 연결
|
218 |
+
inputs = [
|
219 |
+
input_image,
|
220 |
+
brightness_slider,
|
221 |
+
contrast_slider,
|
222 |
+
saturation_slider,
|
223 |
+
temperature_slider,
|
224 |
+
tint_slider,
|
225 |
+
exposure_slider,
|
226 |
+
vibrance_slider,
|
227 |
+
color_mixer_blues_slider,
|
228 |
+
shadows_slider
|
229 |
+
]
|
230 |
+
|
231 |
+
input_components = [
|
232 |
+
brightness_slider,
|
233 |
+
contrast_slider,
|
234 |
+
saturation_slider,
|
235 |
+
temperature_slider,
|
236 |
+
tint_slider,
|
237 |
+
exposure_slider,
|
238 |
+
vibrance_slider,
|
239 |
+
color_mixer_blues_slider,
|
240 |
+
shadows_slider
|
241 |
+
]
|
242 |
+
|
243 |
+
for input_component in input_components:
|
244 |
+
input_component.change(
|
245 |
+
fn=process_image,
|
246 |
+
inputs=inputs,
|
247 |
+
outputs=output_image
|
248 |
+
)
|
249 |
+
|
250 |
+
# 다운로드 버튼 기능
|
251 |
+
download_button.click(
|
252 |
+
fn=download_image,
|
253 |
+
inputs=[output_image, input_image],
|
254 |
+
outputs=download_output
|
255 |
+
)
|
256 |
+
|
257 |
+
return interface
|
258 |
+
|
259 |
+
# 인터페이스 생성 및 실행
|
260 |
+
if __name__ == "__main__":
|
261 |
+
logger.info("애플리케이션 시작")
|
262 |
+
interface = create_interface()
|
263 |
+
interface.queue()
|
264 |
+
interface.launch()
|