NayabShakeel commited on
Commit
1ad8157
·
verified ·
1 Parent(s): d4023fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -45
app.py CHANGED
@@ -1,61 +1,74 @@
 
1
  import streamlit as st
2
- from PIL import Image, ImageEnhance
3
  import numpy as np
4
  import cv2
5
 
6
- # Title and Description
 
 
 
 
 
 
 
7
  st.title("Image Enhancer App")
8
- st.markdown("""
9
- ### Enhance your images with AI-powered tools and essential filters!
10
- Upload an image and apply enhancements like brightness, contrast, sharpness, and more.
11
- """)
12
 
13
  # File Uploader
14
- uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
15
 
16
- if uploaded_file:
 
17
  image = Image.open(uploaded_file)
18
- st.image(image, caption="Original Image", use_column_width=True)
19
-
20
- # Sidebar Filters
21
- st.sidebar.header("Enhancement Options")
22
-
23
- # Brightness
24
- brightness = st.sidebar.slider("Brightness", 0.5, 3.0, 1.0)
25
- enhancer = ImageEnhance.Brightness(image)
26
- image = enhancer.enhance(brightness)
27
-
28
- # Contrast
29
- contrast = st.sidebar.slider("Contrast", 0.5, 3.0, 1.0)
30
- enhancer = ImageEnhance.Contrast(image)
31
- image = enhancer.enhance(contrast)
32
-
33
- # Sharpness
34
- sharpness = st.sidebar.slider("Sharpness", 0.5, 3.0, 1.0)
35
- enhancer = ImageEnhance.Sharpness(image)
36
- image = enhancer.enhance(sharpness)
37
 
38
- # Saturation Adjustment (Using OpenCV for Saturation)
39
- saturation = st.sidebar.slider("Saturation", 0.5, 3.0, 1.0)
 
40
 
41
- # Ensure the image is in a valid NumPy format
42
- image_np = np.array(image).astype(np.uint8)
 
 
 
 
 
 
 
43
 
44
- if len(image_np.shape) == 3: # Check if the image has color channels
45
- image_cv = cv2.cvtColor(image_np, cv2.COLOR_RGB2HSV)
46
- image_cv[:, :, 1] = np.clip(image_cv[:, :, 1] * saturation, 0, 255)
47
- image = Image.fromarray(cv2.cvtColor(image_cv, cv2.COLOR_HSV2RGB))
48
- else:
49
- st.warning("Saturation adjustment is only available for color images.")
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
  # Display Enhanced Image
52
- st.image(image, caption="Enhanced Image", use_column_width=True)
 
53
 
54
- # Save Option
55
- st.sidebar.subheader("Download")
56
- if st.sidebar.button("Download Enhanced Image"):
57
- image.save("enhanced_image.png")
58
- st.sidebar.write("Image saved as `enhanced_image.png`!")
 
 
 
 
59
 
60
- # Footer
61
- st.markdown("Built by NS!")
 
1
+ import os
2
  import streamlit as st
3
+ from PIL import Image
4
  import numpy as np
5
  import cv2
6
 
7
+ # Ensure OpenCV installation
8
+ try:
9
+ import cv2
10
+ except ModuleNotFoundError:
11
+ os.system("pip install opencv-python-headless==4.8.0.74")
12
+ import cv2
13
+
14
+ # Streamlit App Title
15
  st.title("Image Enhancer App")
 
 
 
 
16
 
17
  # File Uploader
18
+ uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
19
 
20
+ if uploaded_file is not None:
21
+ # Load image using PIL and convert to OpenCV format
22
  image = Image.open(uploaded_file)
23
+ image_np = np.array(image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
+ # Display original image
26
+ st.subheader("Original Image")
27
+ st.image(image, caption="Uploaded Image", use_column_width=True)
28
 
29
+ # Enhance Options
30
+ st.subheader("Enhancement Options")
31
+ enhancement = st.selectbox("Choose an enhancement type:", [
32
+ "Brightness Adjustment",
33
+ "Contrast Enhancement",
34
+ "Sharpening",
35
+ "Denoising",
36
+ "Edge Detection"
37
+ ])
38
 
39
+ if enhancement == "Brightness Adjustment":
40
+ brightness = st.slider("Adjust Brightness", -100, 100, 0)
41
+ enhanced_image = cv2.convertScaleAbs(image_np, beta=brightness)
42
+ elif enhancement == "Contrast Enhancement":
43
+ contrast = st.slider("Adjust Contrast", 1.0, 3.0, 1.0)
44
+ alpha = contrast # Simple contrast control
45
+ beta = 0 # Simple brightness control
46
+ enhanced_image = cv2.convertScaleAbs(image_np, alpha=alpha, beta=beta)
47
+ elif enhancement == "Sharpening":
48
+ kernel = np.array([[0, -1, 0],
49
+ [-1, 5, -1],
50
+ [0, -1, 0]])
51
+ enhanced_image = cv2.filter2D(image_np, -1, kernel)
52
+ elif enhancement == "Denoising":
53
+ enhanced_image = cv2.fastNlMeansDenoisingColored(image_np, None, 10, 10, 7, 21)
54
+ elif enhancement == "Edge Detection":
55
+ gray_image = cv2.cvtColor(image_np, cv2.COLOR_BGR2GRAY)
56
+ edges = cv2.Canny(gray_image, 100, 200)
57
+ enhanced_image = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
58
 
59
  # Display Enhanced Image
60
+ st.subheader("Enhanced Image")
61
+ st.image(enhanced_image, caption="Enhanced Image", use_column_width=True)
62
 
63
+ # Download Option
64
+ st.subheader("Download Enhanced Image")
65
+ result_image = Image.fromarray(enhanced_image)
66
+ st.download_button(
67
+ label="Download Image",
68
+ data=result_image.tobytes(),
69
+ file_name="enhanced_image.png",
70
+ mime="image/png"
71
+ )
72
 
73
+ # Hugging Face Integration
74
+ st.markdown("**Note:** Ensure this app is running on Hugging Face Spaces with the required dependencies listed in the `requirements.txt` file.")