File size: 6,959 Bytes
f30bd3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3824a5f
f30bd3e
 
3824a5f
f30bd3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3824a5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import streamlit as st
import cv2
import matplotlib.pyplot as pt
import numpy as np
from PIL import Image
import io
import requests
import base64
from tempfile import NamedTemporaryFile
import logging

logging.basicConfig(filename="logfile.log",
                    format='%(asctime)s %(message)s',
                    filemode='w')

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

st.set_page_config(layout="wide")
st.title("Sketch Generator")
st.caption("Use high resolution image to get optimal results")
      
def verify_url(url):  
    try:
        encoded_content = url.split(',')[1]
        image_data = base64.b64decode(encoded_content)
        image = Image.open(io.BytesIO(image_data))
        logger.info("Valid Base64 Url")
        return True, image
    except Exception as e:
        logger.warning("Please Enter a valid Image url"+str(e))
        return False, None
    
def http_url_image(nk04200111):
    try:
        # Send an HTTP request to the URL
        response = requests.get(nk04200111)
        response.raise_for_status()  # Raise an error for bad responses

        # Open the image using PIL
        image = Image.open(io.BytesIO(response.content))
        logger.info("Valid http Url")
        return True, image

    except Exception as e:
        logger.warning("Please Enter a valid Image url"+str(e))
        return False, None

def process_image(url_string,validation):
    try:
        if validation=="http":
            result,upload = http_url_image(url_string)
        elif validation=="data":
            result,upload = verify_url(url_string)
        if result:
            image_np = np.array(upload)
            firstimg=cv2.cvtColor(image_np,cv2.COLOR_BGR2GRAY)
            blurs=cv2.bitwise_not(firstimg)
            secondimg=cv2.GaussianBlur(blurs, (21, 21),sigmaX=30, sigmaY=30)
            finalimg = cv2.divide(firstimg, 255 - secondimg, scale=256)
            column1,column2,column3,column4=st.columns([1,4,4,1])
            with column1:
                pass
            with column2:
                st.image(upload, caption="Original Image",width=None, use_column_width=None, clamp=False, channels="RGB", output_format="auto")
            with column3:
                if finalimg is not None:
                    st.image(finalimg, caption="Final Image",width=None, use_column_width=None, clamp=False, channels="RGB", output_format="auto")
                            
            with column4:
                pass
            logger.info("Image was processed successfully")
        else:
            st.write(f"The URL {url_string} does not contain a valid image.")
            logger.warning(f"The URL {url_string} does not contain a valid image.")
        return finalimg
    except Exception as e:
        logger.error("Exception occured while processing image: "+str(e))

def get_base64_data(data, filename):
    try:
        base64_data = base64.b64encode(data).decode()
        download_link = f'<a href="data:application/octet-stream;base64,{base64_data}" download="{filename}">Download {filename}</a>'
        logger.info("Download Link created successfully")
        return download_link
    except Exception as e:
        logger.error("Unable to create download link"+str(e))

def save_image(finalimg):
    try:
        NKIMAGES=Image.fromarray(finalimg)
        buffer = io.BytesIO()
        NKIMAGES.save(buffer, format="PNG")
        logger.info("Image was converted successfully")
        # Provide a link for the user to download the processed image
        st.markdown(get_base64_data(buffer.getvalue(), "Final Image.png"), unsafe_allow_html=True)
    except Exception as e:
        logger.error("Image conversion failed"+str(e))
        

def uploadfile():
    try:
        firstimg=None
        secondimg=None
        finalimg=None
        val=1
        selection=st.radio(" ", ["Enter Image Url:link:","Upload Image File:open_file_folder:"], index=None, disabled=False, horizontal=True, label_visibility="visible")
        
        if selection=="Enter Image Url:link:":
            cols1,cols2=st.columns([5,1])
            url_string=st.text_input(" ", type="default", placeholder="Enter the image Url", label_visibility="collapsed")
            if st.button("Generate"):
                if url_string !="" and url_string[:4]=="http":
                    finalimg=process_image(url_string,"http")
                    save_image(finalimg)
                elif url_string !=""and url_string[:4]=="data":
                    finalimg=process_image(url_string,"data")
                    save_image(finalimg)
                else:
                    val=2
            if val==2:
                st.write("Enter a valid image url")
        elif selection=="Upload Image File:open_file_folder:":
            uploader=st.file_uploader("Upload the image here", type=None, accept_multiple_files=False, disabled=False, label_visibility="visible")
            if uploader is None:
                st.write("Please upload an image")
            else:
                upload=uploader.read()
                if st.button("Generate"):
                    image_np = np.array(Image.open(io.BytesIO(upload)))
                    firstimg=cv2.cvtColor(image_np,cv2.COLOR_BGR2GRAY)
                    blurs=cv2.bitwise_not(firstimg)
                    secondimg=cv2.GaussianBlur(blurs, (21, 21),sigmaX=30, sigmaY=30)
                    finalimg = cv2.divide(firstimg, 255 - secondimg, scale=256)
                    
                column1,column2,column3,column4=st.columns([1,4,4,1])
                with column1:
                    pass
                with column2:
                        st.image(upload, caption="Original Image", width=None, use_column_width=None, clamp=False, channels="RGB", output_format="auto")
                with column3:
                    if finalimg is not None:
                        st.image(finalimg, caption="Final Image", width=None, use_column_width=None, clamp=False, channels="RGB", output_format="auto")
                
                with column4:
                    pass
                save_image(finalimg)
                logger.info("Program ran successfully")
    except Exception as e:
        logger.error("Error occured in uploadfile"+str(e))
        
if __name__=="__main__":
    uploadfile()
    footer="""<style>
a:link , a:visited{
color: #0000EE;
background-color: transparent;
text-decoration: underline;
}

a:hover,  a:active {
color: #551A8B;
background-color: transparent;
text-decoration: underline;
}

.footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
height:6%;
background-color: rgba(0,0,0,10);
color: white;
text-align: center;
border-radius:2px;
}
</style>
<div class="footer">
<p>Developed by <a style='display: block; text-align: center; text-decoration:none;' href="https://github.com/nandakumar1052" target="_blank">Nanda Kumar.T</a><p>
</div>
"""
st.markdown(footer,unsafe_allow_html=True)