File size: 3,020 Bytes
b77fd1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import streamlit as st
from PIL import Image
import requests
from io import BytesIO
from streamlit_image_comparison import image_comparison
from utils import upscale_image


def ui():
    image = None
    input_text = None
    uploaded_file = None

    input_area = st.columns([2, 1, 1])

    with input_area[0]:
        option = st.selectbox(
            "How do you want to provide the image?",
            ("Fetch from URL", "Upload from local machine")
        )

    with input_area[2]:
        option_scale = st.selectbox(
            "Which factors do you want to upscale?",
            (2, 3, 4)
        )

    with input_area[1]:
        # , 'SRUNET_interpolation', 'SRUNET_x234_interpolation'
        option_model = st.selectbox(
            "Which model do you want to use?",
            ('SRUNET_x2', 'SRUNET_x3', 'SRUNET_x4', 'SRUNET_x234')
        )

    picture_url_area = st.columns([2, 2], vertical_alignment="top")
    with picture_url_area[0]:
        if option == "Upload from local machine":
            uploaded_file = st.file_uploader(
                "Choose an image...", type=["jpg", "jpeg", "png"])
        elif option == "Fetch from URL":
            input_text = st.text_input("Enter the image URL")

        if st.button("Submit"):
            if option == "Upload from local machine" and uploaded_file is not None:
                try:
                    image = Image.open(uploaded_file)
                    # st.image(image, caption="Uploaded Image", use_column_width=True)
                except Exception as e:
                    st.error(f"Error opening image: {e}")
            elif option == "Fetch from URL" and input_text:
                try:
                    response = requests.get(input_text)
                    response.raise_for_status()
                    image = Image.open(BytesIO(response.content))
                    # st.image(image, caption="Image from URL", use_column_width=True)
                except requests.exceptions.RequestException as e:
                    st.error(f"Error fetching image: {e}")

            if image:
                width, height = image.size
                if width * int(option_scale) > 1000 or height * int(option_scale) > 1000:
                    st.error(
                        "Unable to upscale. The size of upscaled image should be less than 1000x1000")
                    image = None

    if image:
        st.header('Results')
        # image_resize = image.resize((1024, 1024))
        width, height = image.size
        picture_url_area[1].text(
            f"Image size: {width}x{height} --> {width*int(option_scale)}x{height*int(option_scale)}")
        picture_url_area[1].image(
            image, caption="Original", use_column_width=True)

        img_1 = image.resize(
            (width*int(option_scale), height*int(option_scale)), Image.BICUBIC)
        img_2 = upscale_image(image, option_model, int(option_scale))

        image_comparison(
            img1=img_1,
            img2=img_2,
        )