Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +166 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,168 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 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 |
+
# Create a copy of the original dataframe to preserve all columns
|
| 61 |
+
result_df = df.copy()
|
| 62 |
+
|
| 63 |
+
# Get the image column names
|
| 64 |
+
image_columns = [col for col in df.columns if col.startswith('image') and col != 'image1']
|
| 65 |
+
|
| 66 |
+
for i, row in df.iterrows():
|
| 67 |
+
sku = row['sku']
|
| 68 |
+
status_text.text(f"Processing SKU: {sku} ({i+1}/{len(df)})")
|
| 69 |
+
|
| 70 |
+
white_bg_found = False
|
| 71 |
+
img_urls = []
|
| 72 |
+
white_bg_url = None
|
| 73 |
+
|
| 74 |
+
# Check if image1 already has white background
|
| 75 |
+
if pd.notna(row['image1']) and is_white_background(row['image1']):
|
| 76 |
+
white_bg_found = True
|
| 77 |
+
white_bg_url = row['image1']
|
| 78 |
+
|
| 79 |
+
# Collect all other image URLs
|
| 80 |
+
for col in image_columns:
|
| 81 |
+
if pd.notna(row[col]):
|
| 82 |
+
if not white_bg_found and is_white_background(row[col]):
|
| 83 |
+
white_bg_found = True
|
| 84 |
+
white_bg_url = row[col]
|
| 85 |
+
else:
|
| 86 |
+
img_urls.append(row[col])
|
| 87 |
+
|
| 88 |
+
# Reorganize images if white background found
|
| 89 |
+
if white_bg_found:
|
| 90 |
+
# Store the white background image in image1
|
| 91 |
+
result_df.at[i, 'image1'] = white_bg_url
|
| 92 |
+
|
| 93 |
+
# Fill in remaining image slots
|
| 94 |
+
remaining_img_count = 0
|
| 95 |
+
for idx, url in enumerate(img_urls, start=0):
|
| 96 |
+
col_name = f'image{idx+2}'
|
| 97 |
+
if col_name in result_df.columns:
|
| 98 |
+
result_df.at[i, col_name] = url
|
| 99 |
+
remaining_img_count += 1
|
| 100 |
+
|
| 101 |
+
progress_bar.progress((i + 1) / len(df))
|
| 102 |
+
|
| 103 |
+
status_text.text("Processing complete!")
|
| 104 |
+
return result_df
|
| 105 |
+
|
| 106 |
+
def get_download_link(df, filename):
|
| 107 |
+
"""Generate a download link for the dataframe"""
|
| 108 |
+
# Prevent SKUs from being converted to scientific notation
|
| 109 |
+
csv = df.to_csv(index=False, float_format='%.0f')
|
| 110 |
+
b64 = base64.b64encode(csv.encode()).decode()
|
| 111 |
+
href = f'data:file/csv;base64,{b64}'
|
| 112 |
+
return href
|
| 113 |
+
|
| 114 |
+
# File uploader
|
| 115 |
+
uploaded_file = st.file_uploader("Upload your product CSV file", type=["csv"])
|
| 116 |
+
|
| 117 |
+
if uploaded_file is not None:
|
| 118 |
+
# Load and display the uploaded file
|
| 119 |
+
# Treat SKUs as strings to prevent scientific notation issues
|
| 120 |
+
df = pd.read_csv(uploaded_file, dtype={'sku': str})
|
| 121 |
+
|
| 122 |
+
st.subheader("Original Data Preview")
|
| 123 |
+
st.dataframe(df.head())
|
| 124 |
+
|
| 125 |
+
# Check if required columns exist
|
| 126 |
+
required_columns = ['sku', 'image1']
|
| 127 |
+
missing_columns = [col for col in required_columns if col not in df.columns]
|
| 128 |
+
|
| 129 |
+
if missing_columns:
|
| 130 |
+
st.error(f"Missing required columns: {', '.join(missing_columns)}")
|
| 131 |
+
else:
|
| 132 |
+
if st.button("Process Images"):
|
| 133 |
+
with st.spinner("Processing images..."):
|
| 134 |
+
result_df = process_csv(df)
|
| 135 |
+
|
| 136 |
+
st.subheader("Results")
|
| 137 |
+
st.dataframe(result_df.head())
|
| 138 |
+
|
| 139 |
+
# Create download link
|
| 140 |
+
dl_link = get_download_link(result_df, "amazon_optimized_images.csv")
|
| 141 |
+
st.markdown(f'<a href="{dl_link}" download="amazon_optimized_images.csv">Download Processed CSV</a>', unsafe_allow_html=True)
|
| 142 |
+
|
| 143 |
+
# Summary
|
| 144 |
+
total_rows = len(df)
|
| 145 |
+
white_bg_found = sum(1 for _, row in result_df.iterrows() if pd.notna(row['image1']))
|
| 146 |
+
|
| 147 |
+
st.subheader("Summary")
|
| 148 |
+
st.write(f"Total products processed: {total_rows}")
|
| 149 |
+
st.write(f"Products with white background images: {white_bg_found}")
|
| 150 |
+
st.write(f"Products missing white background images: {total_rows - white_bg_found}")
|
| 151 |
+
|
| 152 |
+
# Add instructions and tips
|
| 153 |
+
st.sidebar.header("Instructions")
|
| 154 |
+
st.sidebar.markdown("""
|
| 155 |
+
### CSV Format Requirements
|
| 156 |
+
Your CSV file should include:
|
| 157 |
+
- A 'sku' column with product identifiers
|
| 158 |
+
- Image columns named 'image1', 'image2', etc. containing image URLs
|
| 159 |
+
|
| 160 |
+
### How It Works
|
| 161 |
+
The app analyzes the edges of each image to detect white backgrounds.
|
| 162 |
+
It then reorganizes the URLs to ensure white background images are in the image1 position.
|
| 163 |
|
| 164 |
+
### Tips
|
| 165 |
+
- Ensure your image URLs are publicly accessible
|
| 166 |
+
- The process may take some time for large datasets
|
| 167 |
+
- For best results, make sure product images have clear contrasts between product and background
|
| 168 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|