Gnanashri Kota Venkatesh commited on
Commit
b4469ca
·
unverified ·
1 Parent(s): 8cccd1e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -18
app.py CHANGED
@@ -1209,6 +1209,8 @@ def resize_with_transparent_canvas_tool():
1209
  - Maximum dimension: 4000 pixels
1210
  - For batch processing of many images, please be patient
1211
  """)
 
 
1212
  def center_on_canvas_tool():
1213
  st.header("Center on Transparent Canvas")
1214
  st.markdown("Center small images on a transparent 500x500 canvas.")
@@ -1226,7 +1228,7 @@ def center_on_canvas_tool():
1226
  canvas_size = st.number_input(
1227
  "Canvas size (pixels):",
1228
  min_value=100,
1229
- max_value=2000,
1230
  value=500,
1231
  step=10,
1232
  key="canvas_size_input" # Added unique key
@@ -1359,7 +1361,6 @@ def center_on_canvas_tool():
1359
  - Original aspect ratio is maintained
1360
  """)
1361
 
1362
- # ========== Shared Functions ==========
1363
  def extract_zip_to_temp(uploaded_file):
1364
  """Extract uploaded zip file to temporary directory"""
1365
  temp_dir = tempfile.mkdtemp()
@@ -1416,7 +1417,6 @@ def get_asset_cell(file_path, filename, col_count):
1416
  except Exception as e:
1417
  return Paragraph(f"[Image Error]<br/>{filename}", styles['Normal'])
1418
 
1419
- # ========== PDF Generator Functions ==========
1420
  def analyze_files_by_filename(input_folder):
1421
  """Analyze files by extracting brand names from filenames"""
1422
  dateien = []
@@ -1475,7 +1475,6 @@ def generate_filename_based_pdf_report(input_folder, erste_marke=None):
1475
  nummer_zu_marke = {v: k for k, v in marken_index.items()}
1476
  marken_spalten = sorted(nummer_zu_marke.items())
1477
 
1478
- # Overview by folder
1479
  for folder in sorted(renamed_files_by_folder_and_marke):
1480
  elements.append(Paragraph(f"<b>Folder: {folder}</b>", styles['Heading2']))
1481
 
@@ -1539,7 +1538,6 @@ def generate_filename_based_pdf_report(input_folder, erste_marke=None):
1539
  except Exception as e:
1540
  return None, f"Error generating PDF: {str(e)}"
1541
 
1542
- # ========== Excel Report Functions ==========
1543
  def generate_excel_report(output_folder: Path, marken_index: dict, file_to_factorgroup: dict):
1544
  final_excel_path = output_folder / "IcAt_Overview_Final.xlsx"
1545
 
@@ -1585,7 +1583,6 @@ def generate_excel_report(output_folder: Path, marken_index: dict, file_to_facto
1585
 
1586
  return final_excel_path
1587
 
1588
- # ========== File Processing Functions ==========
1589
  def process_files(input_folder, marken_index, output_root):
1590
  """Process and rename files according to brand numbering"""
1591
  dateien = []
@@ -1605,7 +1602,6 @@ def process_files(input_folder, marken_index, output_root):
1605
  markennummer = marken_index[marke]
1606
  cleaned = get_cleaned_filename_without_brand(file.name, marke)
1607
 
1608
- # Create new filename
1609
  neuer_name = f"{markennummer}B{blocknummer}{marke}{cleaned}{file.suffix.lower()}"
1610
  ziel = output_root / neuer_name
1611
 
@@ -1620,7 +1616,6 @@ def process_files(input_folder, marken_index, output_root):
1620
 
1621
  return renamed_files_by_folder_and_marke, file_to_factorgroup
1622
 
1623
- # ========== Streamlit UI Components ==========
1624
  def brand_renamer_tool():
1625
  st.header("Advanced Brand File Processor")
1626
  st.markdown("Automatically rename and organize brand assets with numbering and generate reports.")
@@ -1633,20 +1628,16 @@ def brand_renamer_tool():
1633
 
1634
  if uploaded_file:
1635
  with st.spinner("Extracting and analyzing files..."):
1636
- # Extract zip file
1637
  temp_dir = extract_zip_to_temp(uploaded_file)
1638
  input_folder = Path(temp_dir)
1639
 
1640
- # Find the actual input folder (in case zip has a root folder)
1641
  items = os.listdir(temp_dir)
1642
  if len(items) == 1 and os.path.isdir(os.path.join(temp_dir, items[0])):
1643
  input_folder = Path(temp_dir) / items[0]
1644
 
1645
- # Create output folder
1646
  output_folder = Path(temp_dir) / "output"
1647
  output_folder.mkdir(exist_ok=True)
1648
 
1649
- # Analyze files to detect brands
1650
  marken_set, _, _ = analyze_files_by_filename(input_folder)
1651
 
1652
  if not marken_set:
@@ -1663,7 +1654,6 @@ def brand_renamer_tool():
1663
  index=0
1664
  )
1665
 
1666
- # Create brand index
1667
  marken_index = {erste_marke: "01"}
1668
  aktuelle_nummer = 2
1669
  for marke in sorted(marken_set):
@@ -1680,15 +1670,12 @@ def brand_renamer_tool():
1680
  output_folder
1681
  )
1682
 
1683
- # Generate PDF report
1684
  pdf_buffer, error = generate_filename_based_pdf_report(input_folder, erste_marke)
1685
  if error:
1686
  st.error(f"PDF generation failed: {error}")
1687
 
1688
- # Generate Excel report
1689
  excel_path = generate_excel_report(output_folder, marken_index, file_to_factorgroup)
1690
 
1691
- # Create zip of processed files
1692
  zip_buffer = io.BytesIO()
1693
  with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
1694
  for file in output_folder.iterdir():
@@ -1696,7 +1683,6 @@ def brand_renamer_tool():
1696
 
1697
  zip_buffer.seek(0)
1698
 
1699
- # Display download options
1700
  st.success("Processing complete!")
1701
 
1702
  col1, col2, col3 = st.columns(3)
@@ -1726,7 +1712,6 @@ def brand_renamer_tool():
1726
  mime="application/zip"
1727
  )
1728
 
1729
- # Clean up
1730
  shutil.rmtree(temp_dir)
1731
  else:
1732
  st.info("👆 Please upload a zip file to get started.")
 
1209
  - Maximum dimension: 4000 pixels
1210
  - For batch processing of many images, please be patient
1211
  """)
1212
+
1213
+
1214
  def center_on_canvas_tool():
1215
  st.header("Center on Transparent Canvas")
1216
  st.markdown("Center small images on a transparent 500x500 canvas.")
 
1228
  canvas_size = st.number_input(
1229
  "Canvas size (pixels):",
1230
  min_value=100,
1231
+ max_value=5000,
1232
  value=500,
1233
  step=10,
1234
  key="canvas_size_input" # Added unique key
 
1361
  - Original aspect ratio is maintained
1362
  """)
1363
 
 
1364
  def extract_zip_to_temp(uploaded_file):
1365
  """Extract uploaded zip file to temporary directory"""
1366
  temp_dir = tempfile.mkdtemp()
 
1417
  except Exception as e:
1418
  return Paragraph(f"[Image Error]<br/>{filename}", styles['Normal'])
1419
 
 
1420
  def analyze_files_by_filename(input_folder):
1421
  """Analyze files by extracting brand names from filenames"""
1422
  dateien = []
 
1475
  nummer_zu_marke = {v: k for k, v in marken_index.items()}
1476
  marken_spalten = sorted(nummer_zu_marke.items())
1477
 
 
1478
  for folder in sorted(renamed_files_by_folder_and_marke):
1479
  elements.append(Paragraph(f"<b>Folder: {folder}</b>", styles['Heading2']))
1480
 
 
1538
  except Exception as e:
1539
  return None, f"Error generating PDF: {str(e)}"
1540
 
 
1541
  def generate_excel_report(output_folder: Path, marken_index: dict, file_to_factorgroup: dict):
1542
  final_excel_path = output_folder / "IcAt_Overview_Final.xlsx"
1543
 
 
1583
 
1584
  return final_excel_path
1585
 
 
1586
  def process_files(input_folder, marken_index, output_root):
1587
  """Process and rename files according to brand numbering"""
1588
  dateien = []
 
1602
  markennummer = marken_index[marke]
1603
  cleaned = get_cleaned_filename_without_brand(file.name, marke)
1604
 
 
1605
  neuer_name = f"{markennummer}B{blocknummer}{marke}{cleaned}{file.suffix.lower()}"
1606
  ziel = output_root / neuer_name
1607
 
 
1616
 
1617
  return renamed_files_by_folder_and_marke, file_to_factorgroup
1618
 
 
1619
  def brand_renamer_tool():
1620
  st.header("Advanced Brand File Processor")
1621
  st.markdown("Automatically rename and organize brand assets with numbering and generate reports.")
 
1628
 
1629
  if uploaded_file:
1630
  with st.spinner("Extracting and analyzing files..."):
 
1631
  temp_dir = extract_zip_to_temp(uploaded_file)
1632
  input_folder = Path(temp_dir)
1633
 
 
1634
  items = os.listdir(temp_dir)
1635
  if len(items) == 1 and os.path.isdir(os.path.join(temp_dir, items[0])):
1636
  input_folder = Path(temp_dir) / items[0]
1637
 
 
1638
  output_folder = Path(temp_dir) / "output"
1639
  output_folder.mkdir(exist_ok=True)
1640
 
 
1641
  marken_set, _, _ = analyze_files_by_filename(input_folder)
1642
 
1643
  if not marken_set:
 
1654
  index=0
1655
  )
1656
 
 
1657
  marken_index = {erste_marke: "01"}
1658
  aktuelle_nummer = 2
1659
  for marke in sorted(marken_set):
 
1670
  output_folder
1671
  )
1672
 
 
1673
  pdf_buffer, error = generate_filename_based_pdf_report(input_folder, erste_marke)
1674
  if error:
1675
  st.error(f"PDF generation failed: {error}")
1676
 
 
1677
  excel_path = generate_excel_report(output_folder, marken_index, file_to_factorgroup)
1678
 
 
1679
  zip_buffer = io.BytesIO()
1680
  with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
1681
  for file in output_folder.iterdir():
 
1683
 
1684
  zip_buffer.seek(0)
1685
 
 
1686
  st.success("Processing complete!")
1687
 
1688
  col1, col2, col3 = st.columns(3)
 
1712
  mime="application/zip"
1713
  )
1714
 
 
1715
  shutil.rmtree(temp_dir)
1716
  else:
1717
  st.info("👆 Please upload a zip file to get started.")