Spaces:
Running
Running
allow the user to rotate the image and change the light
Browse files- modules/streamlit_utils.py +43 -3
modules/streamlit_utils.py
CHANGED
@@ -304,19 +304,59 @@ def load_user_image(img_selected, is_mobile):
|
|
304 |
return uploaded_file
|
305 |
|
306 |
def display_image(uploaded_file, screen_width, is_mobile):
|
307 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
308 |
with st.spinner('Waiting for image display...'):
|
309 |
original_image = get_image(uploaded_file)
|
310 |
resized_image = original_image.resize((screen_width // 2, int(original_image.height * (screen_width // 2) / original_image.width)))
|
311 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
312 |
if not is_mobile:
|
313 |
-
cropped_image = crop_image(
|
314 |
else:
|
315 |
-
st.image(
|
316 |
cropped_image = original_image
|
317 |
|
318 |
return cropped_image
|
319 |
|
|
|
|
|
|
|
320 |
def crop_image(resized_image, original_image):
|
321 |
marge = 10
|
322 |
cropped_box = st_cropper(
|
|
|
304 |
return uploaded_file
|
305 |
|
306 |
def display_image(uploaded_file, screen_width, is_mobile):
|
307 |
+
if 'rotation_angle' not in st.session_state:
|
308 |
+
st.session_state.rotation_angle = 0 # Initialize the rotation angle in session state
|
309 |
+
|
310 |
+
if 'brightness' not in st.session_state:
|
311 |
+
st.session_state.brightness = 1.0 # Initialize brightness in session state
|
312 |
+
|
313 |
+
def rotate_image(angle):
|
314 |
+
st.session_state.rotation_angle += angle
|
315 |
+
|
316 |
+
def adjust_brightness(image, brightness):
|
317 |
+
enhancer = ImageEnhance.Brightness(image)
|
318 |
+
return enhancer.enhance(brightness)
|
319 |
+
|
320 |
with st.spinner('Waiting for image display...'):
|
321 |
original_image = get_image(uploaded_file)
|
322 |
resized_image = original_image.resize((screen_width // 2, int(original_image.height * (screen_width // 2) / original_image.width)))
|
323 |
|
324 |
+
with st.expander("Rotate and adjust brightness"):
|
325 |
+
if not is_mobile:
|
326 |
+
col1, col2 = st.columns([1.5, 1])
|
327 |
+
with col1:
|
328 |
+
st.session_state.brightness = st.slider("Adjust Brightness", min_value=0.2, max_value=2.0, value=1.0, step=0.1)
|
329 |
+
else:
|
330 |
+
st.session_state.brightness = st.slider("Adjust Brightness", min_value=0.2, max_value=2.0, value=1.0, step=0.1)
|
331 |
+
|
332 |
+
# Add buttons to rotate the image next to each other
|
333 |
+
col1, col2 = st.columns([1, 1])
|
334 |
+
with col1:
|
335 |
+
if st.button("Rotate Left"):
|
336 |
+
rotate_image(90)
|
337 |
+
with col2:
|
338 |
+
if st.button("Rotate Right"):
|
339 |
+
rotate_image(-90)
|
340 |
+
|
341 |
+
# Apply the rotation angle from session state
|
342 |
+
rotated_image = resized_image.rotate(st.session_state.rotation_angle, expand=True)
|
343 |
+
original_image = original_image.rotate(st.session_state.rotation_angle, expand=True)
|
344 |
+
|
345 |
+
# Apply the brightness adjustment
|
346 |
+
adjusted_image = adjust_brightness(rotated_image, st.session_state.brightness)
|
347 |
+
original_image = adjust_brightness(original_image, st.session_state.brightness)
|
348 |
+
|
349 |
if not is_mobile:
|
350 |
+
cropped_image = crop_image(adjusted_image, original_image)
|
351 |
else:
|
352 |
+
st.image(adjusted_image, caption="Image", use_column_width=False, width=int(4/5 * screen_width))
|
353 |
cropped_image = original_image
|
354 |
|
355 |
return cropped_image
|
356 |
|
357 |
+
|
358 |
+
|
359 |
+
|
360 |
def crop_image(resized_image, original_image):
|
361 |
marge = 10
|
362 |
cropped_box = st_cropper(
|