KubraBashir commited on
Commit
1590acf
·
verified ·
1 Parent(s): 45ca800

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -49
app.py CHANGED
@@ -1,58 +1,42 @@
1
  import streamlit as st
2
- from PIL import Image, ImageEnhance, ImageFilter, ImageOps
3
  import numpy as np
 
4
 
5
- # Title
6
  st.title("Advanced Image Editor")
7
 
8
- # Sidebar Header
9
- st.sidebar.title("Editing Options")
10
-
11
- # Upload Image
12
- uploaded_file = st.sidebar.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
13
 
14
  if uploaded_file:
15
- # Load the image
16
- img = Image.open(uploaded_file).convert("RGBA") # Support transparency
17
  st.image(img, caption="Original Image", use_column_width=True)
18
 
19
- # Background Color Change
20
- st.sidebar.header("Background Color")
21
- bg_color = st.sidebar.color_picker("Pick a background color", "#FFFFFF")
22
- background = Image.new("RGBA", img.size, bg_color)
23
- img_with_bg = Image.alpha_composite(background, img)
24
 
25
- # Image Transformations
26
- st.sidebar.header("Transformations")
27
  crop_left = st.sidebar.slider("Crop Left (%)", 0, 50, 0)
28
  crop_top = st.sidebar.slider("Crop Top (%)", 0, 50, 0)
29
  crop_right = st.sidebar.slider("Crop Right (%)", 0, 50, 0)
30
  crop_bottom = st.sidebar.slider("Crop Bottom (%)", 0, 50, 0)
 
 
 
 
 
31
 
32
- rotation = st.sidebar.slider("Rotation (°)", 0, 360, 0)
33
- flip_horizontal = st.sidebar.checkbox("Flip Horizontally")
34
- flip_vertical = st.sidebar.checkbox("Flip Vertically")
35
-
36
- # Apply Crop
37
- left = int(crop_left * img_with_bg.width / 100)
38
- top = int(crop_top * img_with_bg.height / 100)
39
- right = img_with_bg.width - int(crop_right * img_with_bg.width / 100)
40
- bottom = img_with_bg.height - int(crop_bottom * img_with_bg.height / 100)
41
- img_cropped = img_with_bg.crop((left, top, right, bottom))
42
-
43
- # Apply Rotation and Flips
44
  img_rotated = img_cropped.rotate(rotation, expand=True)
45
- if flip_horizontal:
46
- img_rotated = img_rotated.transpose(Image.FLIP_LEFT_RIGHT)
47
- if flip_vertical:
48
- img_rotated = img_rotated.transpose(Image.FLIP_TOP_BOTTOM)
49
 
50
- # Snapchat-Like Filters
51
  st.sidebar.header("Filters")
52
- filters = st.sidebar.selectbox(
53
- "Select Filter",
54
- ["None", "Grayscale", "Sepia", "Negative", "Blur", "Contour", "Detail", "Emboss"]
55
- )
56
  if filters == "Grayscale":
57
  img_filtered = img_rotated.convert("L").convert("RGBA")
58
  elif filters == "Sepia":
@@ -72,27 +56,39 @@ if uploaded_file:
72
  else:
73
  img_filtered = img_rotated
74
 
75
- # Enhancements
76
- st.sidebar.header("Enhancements")
77
  brightness = st.sidebar.slider("Brightness", 0.5, 2.0, 1.0)
78
  contrast = st.sidebar.slider("Contrast", 0.5, 2.0, 1.0)
79
  sharpness = st.sidebar.slider("Sharpness", 0.5, 2.0, 1.0)
80
-
81
  img_final = ImageEnhance.Brightness(img_filtered).enhance(brightness)
82
  img_final = ImageEnhance.Contrast(img_final).enhance(contrast)
83
  img_final = ImageEnhance.Sharpness(img_final).enhance(sharpness)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
  # Display Final Image
86
- st.image(img_final, caption="Edited Image", use_column_width=True)
87
 
88
- # Download Option
89
  st.sidebar.header("Download")
90
- img_final = img_final.convert("RGB")
91
- st.sidebar.download_button(
92
- "Download Edited Image",
93
- data=img_final.tobytes(),
94
- file_name="edited_image.jpg",
95
- mime="image/jpeg"
96
- )
97
  else:
98
  st.info("Please upload an image to start editing.")
 
 
1
  import streamlit as st
2
+ from PIL import Image, ImageEnhance, ImageFilter, ImageOps, ImageDraw, ImageFont
3
  import numpy as np
4
+ import io
5
 
6
+ # App Title
7
  st.title("Advanced Image Editor")
8
 
9
+ # File Uploader
10
+ uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
 
 
 
11
 
12
  if uploaded_file:
13
+ # Load image
14
+ img = Image.open(uploaded_file).convert("RGBA")
15
  st.image(img, caption="Original Image", use_column_width=True)
16
 
17
+ # Sidebar Options
18
+ st.sidebar.title("Editing Options")
 
 
 
19
 
20
+ # Crop Tool
21
+ st.sidebar.header("Crop")
22
  crop_left = st.sidebar.slider("Crop Left (%)", 0, 50, 0)
23
  crop_top = st.sidebar.slider("Crop Top (%)", 0, 50, 0)
24
  crop_right = st.sidebar.slider("Crop Right (%)", 0, 50, 0)
25
  crop_bottom = st.sidebar.slider("Crop Bottom (%)", 0, 50, 0)
26
+ left = int(crop_left * img.width / 100)
27
+ top = int(crop_top * img.height / 100)
28
+ right = img.width - int(crop_right * img.width / 100)
29
+ bottom = img.height - int(crop_bottom * img.height / 100)
30
+ img_cropped = img.crop((left, top, right, bottom))
31
 
32
+ # Rotation Tool
33
+ st.sidebar.header("Rotation")
34
+ rotation = st.sidebar.slider("Rotate (°)", 0, 360, 0)
 
 
 
 
 
 
 
 
 
35
  img_rotated = img_cropped.rotate(rotation, expand=True)
 
 
 
 
36
 
37
+ # Filters
38
  st.sidebar.header("Filters")
39
+ filters = st.sidebar.selectbox("Choose a filter", ["None", "Grayscale", "Sepia", "Negative", "Blur", "Contour", "Detail", "Emboss"])
 
 
 
40
  if filters == "Grayscale":
41
  img_filtered = img_rotated.convert("L").convert("RGBA")
42
  elif filters == "Sepia":
 
56
  else:
57
  img_filtered = img_rotated
58
 
59
+ # Adjustments
60
+ st.sidebar.header("Adjustments")
61
  brightness = st.sidebar.slider("Brightness", 0.5, 2.0, 1.0)
62
  contrast = st.sidebar.slider("Contrast", 0.5, 2.0, 1.0)
63
  sharpness = st.sidebar.slider("Sharpness", 0.5, 2.0, 1.0)
64
+ saturation = st.sidebar.slider("Saturation", 0.5, 2.0, 1.0)
65
  img_final = ImageEnhance.Brightness(img_filtered).enhance(brightness)
66
  img_final = ImageEnhance.Contrast(img_final).enhance(contrast)
67
  img_final = ImageEnhance.Sharpness(img_final).enhance(sharpness)
68
+ img_final = ImageEnhance.Color(img_final).enhance(saturation)
69
+
70
+ # Annotations
71
+ st.sidebar.header("Annotations")
72
+ text = st.sidebar.text_input("Add Text")
73
+ if text:
74
+ font = ImageFont.load_default()
75
+ draw = ImageDraw.Draw(img_final)
76
+ draw.text((10, 10), text, fill="white", font=font)
77
+
78
+ # Resize Tool
79
+ st.sidebar.header("Resize")
80
+ new_width = st.sidebar.slider("Width", 50, img_final.width, img_final.width)
81
+ new_height = st.sidebar.slider("Height", 50, img_final.height, img_final.height)
82
+ img_resized = img_final.resize((new_width, new_height))
83
 
84
  # Display Final Image
85
+ st.image(img_resized, caption="Edited Image", use_column_width=True)
86
 
87
+ # Download Button
88
  st.sidebar.header("Download")
89
+ buffer = io.BytesIO()
90
+ img_resized.save(buffer, format="JPEG")
91
+ st.sidebar.download_button("Download Edited Image", buffer.getvalue(), file_name="edited_image.jpg", mime="image/jpeg")
 
 
 
 
92
  else:
93
  st.info("Please upload an image to start editing.")
94
+