-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
54 lines (44 loc) · 1.83 KB
/
app.py
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
import streamlit as st
import cv2
import numpy as np
from PIL import Image
import io
def pencil_sketch(image):
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
inverted_image = cv2.bitwise_not(gray_image)
blurred_image = cv2.GaussianBlur(inverted_image, (21, 21), sigmaX=0, sigmaY=0)
inverted_blurred = cv2.bitwise_not(blurred_image)
sketch = cv2.divide(gray_image, inverted_blurred, scale=256.0)
return sketch
def convert_to_bytes(image):
img = Image.fromarray(image)
buf = io.BytesIO()
img.save(buf, format='PNG')
return buf.getvalue()
st.title("Image to Pencil Sketch Converter")
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
# Read the uploaded image
image = Image.open(uploaded_file)
image_cv = np.array(image)
# Convert the image to pencil sketch
sketch = pencil_sketch(image_cv)
# Create two columns for side-by-side display
col1, col2 = st.columns(2)
with col1:
st.image(image, caption='Original Image', use_column_width=True)
# Convert original image to bytes for download
original_bytes = convert_to_bytes(image_cv)
st.download_button("Download Original Image", original_bytes, "original_image.png")
with col2:
st.image(sketch, caption='Pencil Sketch', use_column_width=True)
# Convert sketch to bytes for download
sketch_bytes = convert_to_bytes(sketch)
st.download_button("Download Pencil Sketch", sketch_bytes, "pencil_sketch.png")
# Button to print the pencil sketch
if st.button("Print Pencil Sketch"):
# This opens the print dialog in the browser
st.markdown(
f'<script>window.print();</script>',
unsafe_allow_html=True
)