Update app.py
Browse files
app.py
CHANGED
@@ -1,159 +1,160 @@
|
|
1 |
-
|
2 |
-
import
|
3 |
-
import
|
4 |
-
|
5 |
-
import
|
6 |
-
import
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
st.
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
with
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
with
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
|
|
|
1 |
+
#
|
2 |
+
import os
|
3 |
+
import zipfile
|
4 |
+
import streamlit as st
|
5 |
+
from PIL import Image, ImageOps
|
6 |
+
import io
|
7 |
+
import tempfile
|
8 |
+
|
9 |
+
# Load pattern images as in-memory objects
|
10 |
+
patterns = {
|
11 |
+
1: "Barcode Pattern.jpg", # Vertical stripes
|
12 |
+
2: "Diagonal Stripes.jpg", # Diagonal stripes
|
13 |
+
3: "horizontal stripe pattern.jpg", # Horizontal stripes
|
14 |
+
4: "Vertical Concentrated.jpg" # Vertical Concentrated stripes
|
15 |
+
}
|
16 |
+
|
17 |
+
# Function to apply selected pattern
|
18 |
+
def apply_pattern(image, pattern_id):
|
19 |
+
"""Apply selected stripe pattern to the given image."""
|
20 |
+
original = image.convert("RGBA")
|
21 |
+
pattern = Image.open(patterns[pattern_id]).convert("L") # Convert pattern to grayscale
|
22 |
+
|
23 |
+
# Create an alpha channel where white becomes transparent and black stays opaque
|
24 |
+
transparent_pattern = Image.new("RGBA", pattern.size)
|
25 |
+
for x in range(pattern.width):
|
26 |
+
for y in range(pattern.height):
|
27 |
+
pixel = pattern.getpixel((x, y))
|
28 |
+
# If the pixel is black (0), set to opaque black; if white (255), set to transparent
|
29 |
+
transparent_pattern.putpixel((x, y), (0, 0, 0, 255) if pixel < 128 else (0, 0, 0, 0))
|
30 |
+
|
31 |
+
# Resize the transparent pattern to match the original image size
|
32 |
+
transparent_pattern = ImageOps.fit(transparent_pattern, original.size, method=0, bleed=0.0, centering=(0.5, 0.5))
|
33 |
+
|
34 |
+
# Overlay the transparent pattern on the original image
|
35 |
+
combined = Image.alpha_composite(original, transparent_pattern)
|
36 |
+
return combined
|
37 |
+
|
38 |
+
# Function to process and save the image with applied pattern
|
39 |
+
def process_image(image, pattern_id, image_name):
|
40 |
+
"""Apply the selected pattern to the uploaded image and save it to the output directory."""
|
41 |
+
combined_image = apply_pattern(image, pattern_id)
|
42 |
+
|
43 |
+
# Convert to RGB if saving as JPEG or PNG
|
44 |
+
combined_image = combined_image.convert("RGB")
|
45 |
+
|
46 |
+
# Save the processed image to a temporary in-memory buffer
|
47 |
+
img_byte_arr = io.BytesIO()
|
48 |
+
combined_image.save(img_byte_arr, format='JPEG')
|
49 |
+
img_byte_arr.seek(0) # Rewind the buffer to the beginning
|
50 |
+
return img_byte_arr
|
51 |
+
|
52 |
+
# Function to create a ZIP file from a list of image paths
|
53 |
+
def create_zip_file(image_buffers, zip_filename):
|
54 |
+
"""Create a ZIP file from the processed images."""
|
55 |
+
with zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
56 |
+
for idx, image_buffer in enumerate(image_buffers):
|
57 |
+
# Save the image in memory with a unique name
|
58 |
+
zipf.writestr(f"patterned_image_{idx + 1}.jpg", image_buffer.read())
|
59 |
+
return zip_filename
|
60 |
+
|
61 |
+
# Streamlit UI
|
62 |
+
st.title("Stripe Pattern Applier")
|
63 |
+
st.write("Upload one or multiple images and apply a stripe pattern.")
|
64 |
+
|
65 |
+
# Radio button for single or multiple image upload
|
66 |
+
upload_option = st.radio("Choose Upload Option", ["Upload Single Image", "Upload Multiple Images"])
|
67 |
+
|
68 |
+
# Upload single or multiple image files based on the selected option
|
69 |
+
if upload_option == "Upload Single Image":
|
70 |
+
uploaded_files = st.file_uploader("Choose an image file", type=["png", "jpg", "jpeg", "bmp", "gif"], key="single_image")
|
71 |
+
else:
|
72 |
+
uploaded_files = st.file_uploader("Choose image files", type=["png", "jpg", "jpeg", "bmp", "gif"], accept_multiple_files=True, key="multiple_images")
|
73 |
+
|
74 |
+
# Allow the user to select a pattern
|
75 |
+
pattern_option = st.radio(
|
76 |
+
"Select Pattern Option",
|
77 |
+
options=["Apply One Pattern", "Apply All Patterns"]
|
78 |
+
)
|
79 |
+
|
80 |
+
# If the user selects "Apply One Pattern", show pattern selection
|
81 |
+
if pattern_option == "Apply One Pattern":
|
82 |
+
pattern_id = st.radio(
|
83 |
+
"Select a stripe pattern",
|
84 |
+
options=[1, 2, 3, 4],
|
85 |
+
format_func=lambda x: {
|
86 |
+
1: "Vertical Stripes",
|
87 |
+
2: "Diagonal Stripes",
|
88 |
+
3: "Horizontal Stripes",
|
89 |
+
4: "Vertical Concentrated Stripes"
|
90 |
+
}[x]
|
91 |
+
)
|
92 |
+
|
93 |
+
# Process and display/download images based on the upload option
|
94 |
+
if uploaded_files:
|
95 |
+
if upload_option == "Upload Single Image":
|
96 |
+
# Process the single image
|
97 |
+
image = Image.open(uploaded_files)
|
98 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
99 |
+
|
100 |
+
# Process and provide download link for the current image
|
101 |
+
if pattern_option == "Apply One Pattern":
|
102 |
+
processed_image_buffer = process_image(image, pattern_id, uploaded_files.name)
|
103 |
+
# Provide the user with a download button for the single image
|
104 |
+
st.download_button(
|
105 |
+
label="Download Processed Image",
|
106 |
+
data=processed_image_buffer,
|
107 |
+
file_name=f"patterned_{uploaded_files.name}", # Name the file with the original name
|
108 |
+
mime="image/jpeg" # MIME type for JPEG. Change if using PNG or another format
|
109 |
+
)
|
110 |
+
else:
|
111 |
+
# Apply all patterns to the single image and provide separate downloads
|
112 |
+
processed_image_buffers = []
|
113 |
+
for pattern_id in patterns:
|
114 |
+
processed_image_buffer = process_image(image, pattern_id, uploaded_files.name)
|
115 |
+
processed_image_buffers.append(processed_image_buffer)
|
116 |
+
|
117 |
+
# Create a ZIP file of all the processed images
|
118 |
+
zip_filename = "processed_images.zip"
|
119 |
+
zip_file = create_zip_file(processed_image_buffers, zip_filename)
|
120 |
+
|
121 |
+
# Provide the user with a download button for the ZIP file
|
122 |
+
with open(zip_filename, "rb") as f:
|
123 |
+
st.download_button(
|
124 |
+
label="Download All Processed Images as ZIP",
|
125 |
+
data=f,
|
126 |
+
file_name="processed_images.zip",
|
127 |
+
mime="application/zip"
|
128 |
+
)
|
129 |
+
|
130 |
+
else:
|
131 |
+
# Create a temporary directory to store processed images
|
132 |
+
processed_image_buffers = []
|
133 |
+
|
134 |
+
# Process and save each image in the directory
|
135 |
+
for uploaded_file in uploaded_files:
|
136 |
+
image = Image.open(uploaded_file)
|
137 |
+
if pattern_option == "Apply One Pattern":
|
138 |
+
processed_image_buffer = process_image(image, pattern_id, uploaded_file.name)
|
139 |
+
processed_image_buffers.append(processed_image_buffer)
|
140 |
+
else:
|
141 |
+
# Apply all patterns to the current image
|
142 |
+
for pattern_id in patterns:
|
143 |
+
processed_image_buffer = process_image(image, pattern_id, uploaded_file.name)
|
144 |
+
processed_image_buffers.append(processed_image_buffer)
|
145 |
+
|
146 |
+
# Create a ZIP file of all the processed images
|
147 |
+
zip_filename = "processed_images.zip"
|
148 |
+
zip_file = create_zip_file(processed_image_buffers, zip_filename)
|
149 |
+
|
150 |
+
# Provide the user with a download button for the ZIP file
|
151 |
+
with open(zip_filename, "rb") as f:
|
152 |
+
st.download_button(
|
153 |
+
label="Download All Processed Images as ZIP",
|
154 |
+
data=f,
|
155 |
+
file_name="processed_images.zip",
|
156 |
+
mime="application/zip"
|
157 |
+
)
|
158 |
+
|
159 |
+
else:
|
160 |
+
st.write("Please upload one or more images to start.")
|