shukdevdatta123 commited on
Commit
3ed9136
·
verified ·
1 Parent(s): 7f52316

Upload 5 files

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