sikandarciv101 commited on
Commit
005fb49
·
verified ·
1 Parent(s): 20de7e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -40
app.py CHANGED
@@ -1,53 +1,65 @@
1
  import streamlit as st
2
- from PIL import Image, ImageDraw, ImageFont, ImageEnhance
3
  import numpy as np
4
  import io
5
 
6
- # Function to create an image with shapes and text
7
- def create_image():
8
- # Create a blank white image (width=500px, height=500px)
9
- img = Image.new('RGB', (500, 500), color='white')
10
- draw = ImageDraw.Draw(img)
11
-
12
- # Draw shapes
13
- draw.rectangle([50, 50, 450, 450], outline="black", width=5) # Rectangle
14
- draw.ellipse([100, 100, 400, 400], outline="blue", width=5) # Circle
15
- draw.line([0, 0, 500, 500], fill="red", width=3) # Diagonal line
16
-
17
- # Add text to the image
18
- font = ImageFont.load_default()
19
- text = "Custom Image"
20
- textwidth, textheight = draw.textsize(text, font)
21
- text_position = ((500 - textwidth) // 2, (500 - textheight) // 2) # Center the text
22
- draw.text(text_position, text, fill="green", font=font)
23
-
24
- return img
25
 
26
  # Function to apply brightness adjustment
27
  def apply_brightness(image, factor):
28
  enhancer = ImageEnhance.Brightness(image)
29
  return enhancer.enhance(factor)
30
 
31
- # Streamlit UI
32
- st.title("Create and Edit Custom Images")
33
- st.write("Create a custom image with shapes and text, and apply filters to it.")
34
-
35
- # Create a custom image
36
- image = create_image()
37
-
38
- # Show the original image
39
- st.image(image, caption="Custom Created Image", use_column_width=True)
40
-
41
- # Apply brightness adjustment
42
- brightness_factor = st.slider("Adjust Brightness", 0.0, 2.0, 1.0)
43
- image = apply_brightness(image, brightness_factor)
44
 
45
- # Show edited image
46
- st.image(image, caption="Edited Custom Image", use_column_width=True)
 
47
 
48
- # Download button for the edited image
49
- buffered = io.BytesIO()
50
- image.save(buffered, format="PNG")
51
- buffered.seek(0)
52
- st.download_button("Download Edited Image", buffered, file_name="custom_edited_image.png", mime="image/png")
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ from PIL import Image, ImageEnhance, ImageDraw, ImageFont
3
  import numpy as np
4
  import io
5
 
6
+ # Function to apply grayscale filter
7
+ def apply_grayscale(image):
8
+ return image.convert("L")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  # Function to apply brightness adjustment
11
  def apply_brightness(image, factor):
12
  enhancer = ImageEnhance.Brightness(image)
13
  return enhancer.enhance(factor)
14
 
15
+ # Function to add text to the image
16
+ def add_text(image, text):
17
+ draw = ImageDraw.Draw(image)
18
+ font = ImageFont.load_default()
19
+ width, height = image.size
20
+ text_width, text_height = draw.textsize(text, font=font)
21
+
22
+ # Position text at the bottom center
23
+ x = (width - text_width) / 2
24
+ y = height - text_height - 10
25
+ draw.text((x, y), text, font=font, fill="white")
26
+ return image
 
27
 
28
+ # Streamlit UI
29
+ st.title("Image Editor")
30
+ st.write("Upload an image and apply filters or add text to it.")
31
 
32
+ # Image upload
33
+ uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
 
 
 
34
 
35
+ if uploaded_image is not None:
36
+ # Open the image using PIL
37
+ image = Image.open(uploaded_image)
38
+
39
+ # Show the original image
40
+ st.image(image, caption="Original Image", use_column_width=True)
41
+
42
+ # Apply filters and editing options
43
+ st.sidebar.title("Edit Options")
44
+
45
+ # Grayscale filter
46
+ if st.sidebar.checkbox("Apply Grayscale"):
47
+ image = apply_grayscale(image)
48
+
49
+ # Brightness adjustment
50
+ brightness_factor = st.sidebar.slider("Adjust Brightness", 0.0, 2.0, 1.0)
51
+ image = apply_brightness(image, brightness_factor)
52
+
53
+ # Add text
54
+ text = st.sidebar.text_input("Add Text to Image")
55
+ if text:
56
+ image = add_text(image, text)
57
+
58
+ # Show edited image
59
+ st.image(image, caption="Edited Image", use_column_width=True)
60
+
61
+ # Download button
62
+ buffered = io.BytesIO()
63
+ image.save(buffered, format="PNG")
64
+ buffered.seek(0)
65
+ st.download_button("Download Edited Image", buffered, file_name="edited_image.png", mime="image/png")