shukdevdatta123 commited on
Commit
150893f
·
verified ·
1 Parent(s): 423b26c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +160 -159
app.py CHANGED
@@ -1,159 +1,160 @@
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.")
 
 
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.")