Junad-Khn1 commited on
Commit
6fdf8d3
·
verified ·
1 Parent(s): cb58839

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image, ImageEnhance, ImageFilter, ImageDraw, ImageFont
3
+ import io
4
+ import numpy as np
5
+ import cv2
6
+ from rembg import remove # For background removal
7
+
8
+ # Title and Description
9
+ st.title("Advanced Image Enhancer and Editor")
10
+ st.write("Enhance, edit, and apply AI-powered enhancements to your images!")
11
+
12
+ # File Upload
13
+ uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
14
+
15
+ if uploaded_file:
16
+ # Load image
17
+ image = Image.open(uploaded_file)
18
+ st.image(image, caption="Uploaded Image", use_column_width=True)
19
+
20
+ # Sidebar Tools
21
+ st.sidebar.title("Editing Tools")
22
+
23
+ # Basic Adjustments
24
+ brightness = st.sidebar.slider("Brightness", 0.1, 3.0, 1.0)
25
+ contrast = st.sidebar.slider("Contrast", 0.1, 3.0, 1.0)
26
+ sharpness = st.sidebar.slider("Sharpness", 0.1, 3.0, 1.0)
27
+ color = st.sidebar.slider("Color Saturation", 0.1, 3.0, 1.0)
28
+
29
+ # Filters
30
+ if st.sidebar.checkbox("Apply Filters"):
31
+ filter_option = st.sidebar.selectbox("Select a Filter", ["None", "BLUR", "DETAIL", "EDGE_ENHANCE", "SMOOTH"])
32
+ if filter_option == "BLUR":
33
+ image = image.filter(ImageFilter.BLUR)
34
+ elif filter_option == "DETAIL":
35
+ image = image.filter(ImageFilter.DETAIL)
36
+ elif filter_option == "EDGE_ENHANCE":
37
+ image = image.filter(ImageFilter.EDGE_ENHANCE)
38
+ elif filter_option == "SMOOTH":
39
+ image = image.filter(ImageFilter.SMOOTH)
40
+
41
+ # AI Filters
42
+ if st.sidebar.checkbox("Apply AI Filters"):
43
+ ai_filter = st.sidebar.selectbox("Select AI Filter", ["Cartoonize", "Sketch"])
44
+ image_array = np.array(image)
45
+
46
+ if ai_filter == "Cartoonize":
47
+ gray = cv2.cvtColor(image_array, cv2.COLOR_BGR2GRAY)
48
+ edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 9)
49
+ color = cv2.bilateralFilter(image_array, 9, 250, 250)
50
+ cartoon = cv2.bitwise_and(color, color, mask=edges)
51
+ image = Image.fromarray(cartoon)
52
+
53
+ elif ai_filter == "Sketch":
54
+ gray = cv2.cvtColor(image_array, cv2.COLOR_BGR2GRAY)
55
+ inv = 255 - gray
56
+ blur = cv2.GaussianBlur(inv, (21, 21), 0)
57
+ sketch = cv2.divide(gray, 255 - blur, scale=256)
58
+ image = Image.fromarray(sketch)
59
+
60
+ # Background Removal
61
+ if st.sidebar.checkbox("Remove Background"):
62
+ image = Image.open(io.BytesIO(remove(uploaded_file.read())))
63
+
64
+ # Watermarking
65
+ if st.sidebar.checkbox("Add Watermark"):
66
+ watermark_text = st.sidebar.text_input("Watermark Text", "My Watermark")
67
+ draw = ImageDraw.Draw(image)
68
+ font = ImageFont.truetype("arial.ttf", 36)
69
+ width, height = image.size
70
+ text_width, text_height = draw.textsize(watermark_text, font)
71
+ x, y = width - text_width - 10, height - text_height - 10
72
+ draw.text((x, y), watermark_text, font=font, fill=(255, 255, 255, 128))
73
+
74
+ # Enhance Image
75
+ enhancer = ImageEnhance.Brightness(image)
76
+ image = enhancer.enhance(brightness)
77
+ enhancer = ImageEnhance.Contrast(image)
78
+ image = enhancer.enhance(contrast)
79
+ enhancer = ImageEnhance.Sharpness(image)
80
+ image = enhancer.enhance(sharpness)
81
+ enhancer = ImageEnhance.Color(image)
82
+ image = enhancer.enhance(color)
83
+
84
+ # Display Enhanced Image
85
+ st.image(image, caption="Enhanced Image", use_column_width=True)
86
+
87
+ # Download Button
88
+ buf = io.BytesIO()
89
+ image.save(buf, format="PNG")
90
+ byte_im = buf.getvalue()
91
+ st.download_button("Download Enhanced Image", data=byte_im, file_name="enhanced_image.png", mime="image/png")