Nanda Kumar commited on
Commit
f30bd3e
1 Parent(s): f5284b1

First Build

Browse files
Files changed (4) hide show
  1. README.md +4 -1
  2. app.py +153 -0
  3. logfile.log +6 -0
  4. requirements.txt +53 -0
README.md CHANGED
@@ -1,2 +1,5 @@
1
- # Sketch-Generator
2
  Convert images to sketch Using python and streamlit library
 
 
 
 
1
+ # Sketch Generator
2
  Convert images to sketch Using python and streamlit library
3
+
4
+ # Suggession
5
+ Try using high resolution images to get good results also the application does not perform well on images in dark theme.
app.py ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import cv2
3
+ import matplotlib.pyplot as pt
4
+ import numpy as np
5
+ from PIL import Image
6
+ import io
7
+ import requests
8
+ import base64
9
+ from tempfile import NamedTemporaryFile
10
+ import logging
11
+
12
+ logging.basicConfig(filename="logfile.log",
13
+ format='%(asctime)s %(message)s',
14
+ filemode='w')
15
+
16
+ logger = logging.getLogger()
17
+ logger.setLevel(logging.DEBUG)
18
+
19
+ st.set_page_config(layout="wide")
20
+ st.title("Sketch Generator")
21
+ st.caption("Use high resolution image to get optimal results")
22
+
23
+ def verify_url(url):
24
+ try:
25
+ encoded_content = url.split(',')[1]
26
+ image_data = base64.b64decode(encoded_content)
27
+ image = Image.open(io.BytesIO(image_data))
28
+ logger.info("Valid Base64 Url")
29
+ return True, image
30
+ except Exception as e:
31
+ logger.warning("Please Enter a valid Image url"+str(e))
32
+ return False, None
33
+
34
+ def http_url_image(nk04200111):
35
+ try:
36
+ # Send an HTTP request to the URL
37
+ response = requests.get(nk04200111)
38
+ response.raise_for_status() # Raise an error for bad responses
39
+
40
+ # Open the image using PIL
41
+ image = Image.open(io.BytesIO(response.content))
42
+ logger.info("Valid http Url")
43
+ return True, image
44
+
45
+ except Exception as e:
46
+ logger.warning("Please Enter a valid Image url"+str(e))
47
+ return False, None
48
+
49
+ def process_image(url_string,validation):
50
+ try:
51
+ if validation=="http":
52
+ result,upload = http_url_image(url_string)
53
+ elif validation=="data":
54
+ result,upload = verify_url(url_string)
55
+ if result:
56
+ image_np = np.array(upload)
57
+ firstimg=cv2.cvtColor(image_np,cv2.COLOR_BGR2GRAY)
58
+ blurs=cv2.bitwise_not(firstimg)
59
+ secondimg=cv2.GaussianBlur(blurs, (21, 21),sigmaX=30, sigmaY=30)
60
+ finalimg = cv2.divide(firstimg, 255 - secondimg, scale=256)
61
+ column1,column2,column3,column4=st.columns([1,4,4,1])
62
+ with column1:
63
+ pass
64
+ with column2:
65
+ st.image(upload, caption="Original Image", width=600, use_column_width=None, clamp=False, channels="RGB", output_format="auto")
66
+ with column3:
67
+ if finalimg is not None:
68
+ st.image(finalimg, caption="Final Image", width=600, use_column_width=None, clamp=False, channels="RGB", output_format="auto")
69
+
70
+ with column4:
71
+ pass
72
+ logger.info("Image was processed successfully")
73
+ else:
74
+ st.write(f"The URL {url_string} does not contain a valid image.")
75
+ logger.warning(f"The URL {url_string} does not contain a valid image.")
76
+ return finalimg
77
+ except Exception as e:
78
+ logger.error("Exception occured while processing image: "+str(e))
79
+
80
+ def get_base64_data(data, filename):
81
+ try:
82
+ base64_data = base64.b64encode(data).decode()
83
+ download_link = f'<a href="data:application/octet-stream;base64,{base64_data}" download="{filename}">Download {filename}</a>'
84
+ logger.info("Download Link created successfully")
85
+ return download_link
86
+ except Exception as e:
87
+ logger.error("Unable to create download link"+str(e))
88
+
89
+ def save_image(finalimg):
90
+ try:
91
+ NKIMAGES=Image.fromarray(finalimg)
92
+ buffer = io.BytesIO()
93
+ NKIMAGES.save(buffer, format="PNG")
94
+ logger.info("Image was converted successfully")
95
+ # Provide a link for the user to download the processed image
96
+ st.markdown(get_base64_data(buffer.getvalue(), "Final Image.png"), unsafe_allow_html=True)
97
+ except Exception as e:
98
+ logger.error("Image conversion failed"+str(e))
99
+
100
+
101
+ def uploadfile():
102
+ try:
103
+ firstimg=None
104
+ secondimg=None
105
+ finalimg=None
106
+ val=1
107
+ selection=st.radio(" ", ["Enter Image Url:link:","Upload Image File:open_file_folder:"], index=None, disabled=False, horizontal=True, label_visibility="visible")
108
+
109
+ if selection=="Enter Image Url:link:":
110
+ cols1,cols2=st.columns([5,1])
111
+ url_string=st.text_input(" ", type="default", placeholder="Enter the image Url", label_visibility="collapsed")
112
+ if st.button("Generate"):
113
+ if url_string !="" and url_string[:4]=="http":
114
+ finalimg=process_image(url_string,"http")
115
+ save_image(finalimg)
116
+ elif url_string !=""and url_string[:4]=="data":
117
+ finalimg=process_image(url_string,"data")
118
+ save_image(finalimg)
119
+ else:
120
+ val=2
121
+ if val==2:
122
+ st.write("Enter a valid image url")
123
+ elif selection=="Upload Image File:open_file_folder:":
124
+ uploader=st.file_uploader("Upload the image here", type=None, accept_multiple_files=False, disabled=False, label_visibility="visible")
125
+ if uploader is None:
126
+ st.write("Please upload an image")
127
+ else:
128
+ upload=uploader.read()
129
+ if st.button("Generate"):
130
+ image_np = np.array(Image.open(io.BytesIO(upload)))
131
+ firstimg=cv2.cvtColor(image_np,cv2.COLOR_BGR2GRAY)
132
+ blurs=cv2.bitwise_not(firstimg)
133
+ secondimg=cv2.GaussianBlur(blurs, (21, 21),sigmaX=30, sigmaY=30)
134
+ finalimg = cv2.divide(firstimg, 255 - secondimg, scale=256)
135
+
136
+ column1,column2,column3,column4=st.columns([1,4,4,1])
137
+ with column1:
138
+ pass
139
+ with column2:
140
+ st.image(upload, caption="Original Image", width=None, use_column_width=None, clamp=False, channels="RGB", output_format="auto")
141
+ with column3:
142
+ if finalimg is not None:
143
+ st.image(finalimg, caption="Final Image", width=None, use_column_width=None, clamp=False, channels="RGB", output_format="auto")
144
+
145
+ with column4:
146
+ pass
147
+ save_image(finalimg)
148
+ logger.info("Program ran successfully")
149
+ except Exception as e:
150
+ logger.error("Error occured in uploadfile"+str(e))
151
+
152
+ if __name__=="__main__":
153
+ uploadfile()
logfile.log ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ 2023-12-04 22:42:07,304 Starting new HTTPS connection (1): m.media-amazon.com:443
2
+ 2023-12-04 22:42:07,573 https://m.media-amazon.com:443 "GET /images/I/81eUYDkAdcL.jpg HTTP/1.1" 200 469714
3
+ 2023-12-04 22:42:07,624 Valid http Url
4
+ 2023-12-04 22:42:07,658 Image was processed successfully
5
+ 2023-12-04 22:42:07,716 Image was converted successfully
6
+ 2023-12-04 22:42:07,718 Download Link created successfully
requirements.txt ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ altair==5.2.0
2
+ attrs==23.1.0
3
+ blinker==1.7.0
4
+ cachetools==5.3.2
5
+ certifi==2023.11.17
6
+ charset-normalizer==3.3.2
7
+ click==8.1.7
8
+ colorama==0.4.6
9
+ contourpy==1.2.0
10
+ cycler==0.12.1
11
+ fonttools==4.46.0
12
+ gitdb==4.0.11
13
+ GitPython==3.1.40
14
+ idna==3.6
15
+ importlib-metadata==6.9.0
16
+ Jinja2==3.1.2
17
+ jsonschema==4.20.0
18
+ jsonschema-specifications==2023.11.2
19
+ kiwisolver==1.4.5
20
+ markdown-it-py==3.0.0
21
+ MarkupSafe==2.1.3
22
+ matplotlib==3.8.2
23
+ mdurl==0.1.2
24
+ numpy==1.26.2
25
+ opencv-python==4.8.1.78
26
+ packaging==23.2
27
+ pandas==2.1.3
28
+ Pillow==10.1.0
29
+ protobuf==4.25.1
30
+ pyarrow==14.0.1
31
+ pydeck==0.8.1b0
32
+ Pygments==2.17.2
33
+ pyparsing==3.1.1
34
+ python-dateutil==2.8.2
35
+ pytz==2023.3.post1
36
+ referencing==0.31.1
37
+ requests==2.31.0
38
+ rich==13.7.0
39
+ rpds-py==0.13.2
40
+ six==1.16.0
41
+ smmap==5.0.1
42
+ streamlit==1.29.0
43
+ tenacity==8.2.3
44
+ toml==0.10.2
45
+ toolz==0.12.0
46
+ tornado==6.4
47
+ typing_extensions==4.8.0
48
+ tzdata==2023.3
49
+ tzlocal==5.2
50
+ urllib3==2.1.0
51
+ validators==0.22.0
52
+ watchdog==3.0.0
53
+ zipp==3.17.0