Spaces:
Runtime error
Runtime error
create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import streamlit as st
|
3 |
+
import asyncio
|
4 |
+
from PIL import Image
|
5 |
+
from app.upscale import ImageUpscaler
|
6 |
+
|
7 |
+
img_upscale = ImageUpscaler()
|
8 |
+
|
9 |
+
async def async_upscaler(image_stream, factor):
|
10 |
+
return await img_upscale.upscaler(target_img=image_stream, factor=factor, byte_stream=True)
|
11 |
+
|
12 |
+
def calculate_new_dimension(original_size, entered_size, dimension_type):
|
13 |
+
original_width, original_height = original_size
|
14 |
+
aspect_ratio = original_width / original_height
|
15 |
+
|
16 |
+
if dimension_type == 'width':
|
17 |
+
new_width = entered_size
|
18 |
+
new_height = round(new_width / aspect_ratio)
|
19 |
+
return new_width, new_height
|
20 |
+
else:
|
21 |
+
new_height = entered_size
|
22 |
+
new_width = round(new_height * aspect_ratio)
|
23 |
+
return new_width, new_height
|
24 |
+
|
25 |
+
def calculate_factor(original_size, target_size):
|
26 |
+
return target_size[0] / original_size[0], target_size[1] / original_size[1]
|
27 |
+
|
28 |
+
def main():
|
29 |
+
if 'user_factor' not in st.session_state:
|
30 |
+
st.session_state.user_factor = None
|
31 |
+
|
32 |
+
password = st.text_input("Enter Password", type='password')
|
33 |
+
if password != '1bz@123':
|
34 |
+
st.stop()
|
35 |
+
|
36 |
+
cols = st.columns([3,5,2])
|
37 |
+
logo_url = 'https://media.licdn.com/dms/image/D560BAQGQxI71sZ1vTQ/company-logo_200_200/0/1687829291546?e=1703721600&v=beta&t=5RnOK7-Ak8cj1EQose8HQHWUYXPXRpI-x2dnjGjUABg'
|
38 |
+
cols[0].image(logo_url, width=int(200 * 0.8))
|
39 |
+
cols[1].markdown("<h1 style='text-align: left; color: black;'>IMAGE UPSCALER</h1>", unsafe_allow_html=True)
|
40 |
+
cols[1].markdown("This is a PoC application for upscaling images.")
|
41 |
+
cols[1].markdown("[onebyzero.ai](http://www.onebyzero.ai)", unsafe_allow_html=True)
|
42 |
+
|
43 |
+
uploaded_image = st.file_uploader('Upload target image (Required)', type=['jpeg', 'jpg', 'png'])
|
44 |
+
|
45 |
+
if uploaded_image is not None:
|
46 |
+
image = Image.open(uploaded_image)
|
47 |
+
Image_stream = img_upscale.pil2byte(image)
|
48 |
+
original_size = image.size
|
49 |
+
st.write(f"Original Image Resolution: {original_size[0]} x {original_size[1]}")
|
50 |
+
|
51 |
+
col1, mid_col, col2 = st.columns([3, 1, 3])
|
52 |
+
with col1:
|
53 |
+
dimension_type = st.radio("Dimension Type:", ('width', 'height'))
|
54 |
+
entered_size = st.number_input("Enter Target Width or Height:", min_value=1, value=original_size[0])
|
55 |
+
if st.button("Calculate Target Resolution"):
|
56 |
+
if dimension_type == 'width':
|
57 |
+
target_size = calculate_new_dimension(original_size, entered_size, 'width')
|
58 |
+
else:
|
59 |
+
target_size = calculate_new_dimension(original_size, entered_size, 'height')
|
60 |
+
st.write(f"Target Resolution: {target_size[0]} x {target_size[1]}")
|
61 |
+
factor_x, factor_y = calculate_factor(original_size, target_size)
|
62 |
+
st.session_state.user_factor = round(min(factor_x, factor_y), 2)
|
63 |
+
st.write(f"Calculated Upscaling Factor: {st.session_state.user_factor}")
|
64 |
+
|
65 |
+
with mid_col:
|
66 |
+
st.markdown("<h2 style='text-align: center;'>OR</h2>", unsafe_allow_html=True)
|
67 |
+
|
68 |
+
with col2:
|
69 |
+
factor_slider = st.slider('Choose Upscaling Factor:', min_value=1.0, max_value=10.0, value=1.0, step=0.5, format="")
|
70 |
+
st.write(f"Selected Upscaling Factor: {factor_slider}")
|
71 |
+
|
72 |
+
if st.button("Generate"):
|
73 |
+
factor = st.session_state.user_factor if st.session_state.user_factor is not None else factor_slider
|
74 |
+
with st.spinner('Generating images...'):
|
75 |
+
result_image_stream = asyncio.run(async_upscaler(Image_stream, factor))
|
76 |
+
try:
|
77 |
+
st.image(result_image_stream.getvalue())
|
78 |
+
except Exception as e:
|
79 |
+
st.error(f"An error occurred during image upscaling: {e}")
|
80 |
+
|
81 |
+
if __name__ == "__main__":
|
82 |
+
main()
|
83 |
+
st.markdown(""" --- """)
|
84 |
+
st.markdown("<h6 style='text-align: left; color: grey;'>Created by <a href='mailto:[email protected]'>Sandeep Patra</a></h6>", unsafe_allow_html=True)
|