Neerajbss commited on
Commit
5a30ffd
·
verified ·
1 Parent(s): 21e4d60

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +168 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,170 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
1
  import streamlit as st
2
+ import pandas as pd
3
+ import requests
4
+ from PIL import Image, ImageStat
5
+ from io import BytesIO
6
+ import numpy as np
7
+ import time
8
+ import base64
9
+
10
+ st.set_page_config(page_title="Amazon Image Optimizer", layout="wide")
11
+
12
+ st.title("Amazon Image Optimizer")
13
+ st.markdown("""
14
+ This app helps reorganize your product images to comply with Amazon's listing requirements.
15
+ Upload your CSV file containing SKU and image URLs, and the app will:
16
+ 1. Detect which image has a white background
17
+ 2. Ensure the white background image is placed in the image1 column
18
+ 3. Allow you to download the reorganized CSV
19
+ """)
20
+
21
+ def is_white_background(image_url, threshold=240):
22
+ """Determine if an image has a predominantly white background"""
23
+ try:
24
+ response = requests.get(image_url, timeout=5)
25
+ img = Image.open(BytesIO(response.content)).convert('RGB')
26
+
27
+ # Get the edges of the image (10% from each border)
28
+ width, height = img.size
29
+ border_width = int(width * 0.1)
30
+ border_height = int(height * 0.1)
31
+
32
+ # Create masks for the edges
33
+ left_edge = img.crop((0, 0, border_width, height))
34
+ right_edge = img.crop((width - border_width, 0, width, height))
35
+ top_edge = img.crop((0, 0, width, border_height))
36
+ bottom_edge = img.crop((0, height - border_height, width, height))
37
+
38
+ # Calculate average RGB values for edges
39
+ edges = [left_edge, right_edge, top_edge, bottom_edge]
40
+ edge_stats = [ImageStat.Stat(edge) for edge in edges]
41
+ edge_means = [stat.mean for stat in edge_stats]
42
+
43
+ # Check if edges are predominantly white
44
+ is_white = all(all(channel > threshold for channel in mean) for mean in edge_means)
45
+
46
+ # Also check overall brightness
47
+ overall_stat = ImageStat.Stat(img)
48
+ overall_brightness = sum(overall_stat.mean) / 3
49
+
50
+ return is_white and overall_brightness > threshold
51
+ except Exception as e:
52
+ st.error(f"Error processing image: {e}")
53
+ return False
54
+
55
+ def process_csv(df):
56
+ """Process the dataframe to reorder images putting white background first"""
57
+ progress_bar = st.progress(0)
58
+ status_text = st.empty()
59
+
60
+ # Get the image column names
61
+ image_columns = [col for col in df.columns if col.startswith('image') and col != 'image1']
62
+
63
+ results = []
64
+
65
+ for i, row in df.iterrows():
66
+ sku = row['sku']
67
+ status_text.text(f"Processing SKU: {sku} ({i+1}/{len(df)})")
68
+
69
+ white_bg_found = False
70
+ img_urls = []
71
+ white_bg_url = None
72
+
73
+ # Check if image1 already has white background
74
+ if pd.notna(row['image1']) and is_white_background(row['image1']):
75
+ white_bg_found = True
76
+ white_bg_url = row['image1']
77
+
78
+ # Collect all other image URLs
79
+ for col in image_columns:
80
+ if pd.notna(row[col]):
81
+ if not white_bg_found and is_white_background(row[col]):
82
+ white_bg_found = True
83
+ white_bg_url = row[col]
84
+ else:
85
+ img_urls.append(row[col])
86
+
87
+ # Create new row with reorganized images
88
+ new_row = {'sku': sku}
89
+
90
+ # Put white background image first if found
91
+ if white_bg_found:
92
+ new_row['image1'] = white_bg_url
93
+
94
+ # Fill in remaining image slots
95
+ for idx, url in enumerate(img_urls, start=2):
96
+ if idx <= 13: # Limiting to image13
97
+ new_row[f'image{idx}'] = url
98
+ else:
99
+ # If no white background found, keep original order
100
+ new_row['image1'] = row['image1'] if pd.notna(row['image1']) else None
101
+ for col in image_columns:
102
+ new_row[col] = row[col] if pd.notna(row[col]) else None
103
+
104
+ results.append(new_row)
105
+ progress_bar.progress((i + 1) / len(df))
106
+
107
+ status_text.text("Processing complete!")
108
+ return pd.DataFrame(results)
109
+
110
+ def get_download_link(df, filename):
111
+ """Generate a download link for the dataframe"""
112
+ csv = df.to_csv(index=False)
113
+ b64 = base64.b64encode(csv.encode()).decode()
114
+ href = f'data:file/csv;base64,{b64}'
115
+ return href
116
+
117
+ # File uploader
118
+ uploaded_file = st.file_uploader("Upload your product CSV file", type=["csv"])
119
+
120
+ if uploaded_file is not None:
121
+ # Load and display the uploaded file
122
+ df = pd.read_csv(uploaded_file)
123
+
124
+ st.subheader("Original Data Preview")
125
+ st.dataframe(df.head())
126
+
127
+ # Check if required columns exist
128
+ required_columns = ['sku', 'image1']
129
+ missing_columns = [col for col in required_columns if col not in df.columns]
130
+
131
+ if missing_columns:
132
+ st.error(f"Missing required columns: {', '.join(missing_columns)}")
133
+ else:
134
+ if st.button("Process Images"):
135
+ with st.spinner("Processing images..."):
136
+ result_df = process_csv(df)
137
+
138
+ st.subheader("Results")
139
+ st.dataframe(result_df.head())
140
+
141
+ # Create download link
142
+ dl_link = get_download_link(result_df, "amazon_optimized_images.csv")
143
+ st.markdown(f'<a href="{dl_link}" download="amazon_optimized_images.csv">Download Processed CSV</a>', unsafe_allow_html=True)
144
+
145
+ # Summary
146
+ total_rows = len(df)
147
+ white_bg_found = sum(1 for _, row in result_df.iterrows() if pd.notna(row['image1']))
148
+
149
+ st.subheader("Summary")
150
+ st.write(f"Total products processed: {total_rows}")
151
+ st.write(f"Products with white background images: {white_bg_found}")
152
+ st.write(f"Products missing white background images: {total_rows - white_bg_found}")
153
+
154
+ # Add instructions and tips
155
+ st.sidebar.header("Instructions")
156
+ st.sidebar.markdown("""
157
+ ### CSV Format Requirements
158
+ Your CSV file should include:
159
+ - A 'sku' column with product identifiers
160
+ - Image columns named 'image1', 'image2', etc. containing image URLs
161
+
162
+ ### How It Works
163
+ The app analyzes the edges of each image to detect white backgrounds.
164
+ It then reorganizes the URLs to ensure white background images are in the image1 position.
165
 
166
+ ### Tips
167
+ - Ensure your image URLs are publicly accessible
168
+ - The process may take some time for large datasets
169
+ - For best results, make sure product images have clear contrasts between product and background
170
+ """)