-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBGR2Gray_app.py
More file actions
53 lines (43 loc) · 1.71 KB
/
BGR2Gray_app.py
File metadata and controls
53 lines (43 loc) · 1.71 KB
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
import streamlit as st
import cv2
import numpy as np
from PIL import Image
import io
st.set_page_config(page_title="Grayscale Converter", page_icon="🖤", layout="wide")
st.set_page_config(page_title="Grayscale Image Converter", layout="wide")
st.title("🖼️ Grayscale Image Converter")
# 1. Upload the image
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
# 2. Load the image using PIL, then convert to OpenCV format
image = Image.open(uploaded_file)
img_rgb = np.array(image.convert("RGB")) # Convert to RGB
img_bgr = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2BGR) # Convert to BGR for OpenCV
# 3. Convert to grayscale
gray = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)
# 4. Display side-by-side images using columns
col1, col2 = st.columns(2)
with col1:
st.subheader("Original Image")
st.image(image, use_container_width=True)
with col2:
st.subheader("Grayscale Image")
st.image(gray, use_container_width=True, clamp=True, channels="GRAY")
# 5. Download option
st.markdown("---")
st.subheader("💾 Download Grayscale Image")
filename = st.text_input(
"Enter filename (without extension)", value="grayscale_image"
)
if filename:
# Convert grayscale image to PIL and then to byte stream
gray_image_pil = Image.fromarray(gray)
buf = io.BytesIO()
gray_image_pil.save(buf, format="PNG")
byte_im = buf.getvalue()
st.download_button(
label="Download as PNG",
data=byte_im,
file_name=filename + ".png",
mime="image/png",
)