ThisHumanA commited on
Commit
ce53ba0
·
verified ·
1 Parent(s): 88dacd4

Upload image_sharpner.py

Browse files
Files changed (1) hide show
  1. image_sharpner.py +74 -0
image_sharpner.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import cv2
3
+ import numpy as np
4
+ from PIL import Image
5
+ import io
6
+ import os
7
+
8
+ # Function to sharpen the image
9
+ def sharpen_image(image, strength):
10
+ kernel = np.array([[0, -strength, 0],
11
+ [-strength, 1 + 4 * strength, -strength],
12
+ [0, -strength, 0]])
13
+ sharpened = cv2.filter2D(image, -1, kernel)
14
+ return sharpened
15
+
16
+ # Function to smooth the image
17
+ def smooth_image(image, strength):
18
+ ksize = int(2 * round(strength) + 1) # Kernel size must be odd
19
+ smoothed = cv2.GaussianBlur(image, (ksize, ksize), 0)
20
+ return smoothed
21
+
22
+ # Streamlit App
23
+ st.title("Image Sharpening and Smoothing Tool")
24
+
25
+ # File uploader
26
+ uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
27
+
28
+ if uploaded_file:
29
+ # Read the uploaded image
30
+ original_name = os.path.splitext(uploaded_file.name)[0] # Extract file name without extension
31
+ image = Image.open(uploaded_file)
32
+ image_array = np.array(image)
33
+
34
+ # Display the original image
35
+ st.subheader("Original Image")
36
+ st.image(image_array, channels="RGB")
37
+
38
+ # Choose between sharpening or smoothing
39
+ choice = st.radio("Choose an option:", ["Sharpen", "Smooth"])
40
+
41
+ # Slider for strength adjustment
42
+ if choice == "Sharpen":
43
+ strength = st.slider("Sharpening Strength", min_value=0.0, max_value=2.0, value=0.5, step=0.1)
44
+ processed_image = sharpen_image(image_array, strength)
45
+ suffix = "_sharpened"
46
+ else:
47
+ strength = st.slider("Smoothing Strength", min_value=1.0, max_value=10.0, value=3.0, step=1.0)
48
+ processed_image = smooth_image(image_array, strength)
49
+ suffix = "_smoothed"
50
+
51
+ # Display the processed image
52
+ st.subheader("Processed Image")
53
+ st.image(processed_image, channels="RGB")
54
+
55
+ # Convert the processed image to PIL format for downloading
56
+ processed_pil_image = Image.fromarray(processed_image)
57
+ if processed_pil_image.mode != "RGB":
58
+ processed_pil_image = processed_pil_image.convert("RGB")
59
+
60
+ # Prepare the image for download
61
+ buffer = io.BytesIO()
62
+ processed_pil_image.save(buffer, format="JPEG")
63
+ buffer.seek(0)
64
+
65
+ # Set the download filename
66
+ download_filename = f"{original_name}{suffix}.jpg"
67
+
68
+ # Download button
69
+ st.download_button(
70
+ label="Download Processed Image",
71
+ data=buffer,
72
+ file_name=download_filename,
73
+ mime="image/jpeg"
74
+ )