Spaces:
Sleeping
Sleeping
venkatesh commited on
Commit ยท
445a772
1
Parent(s): 672e740
changes on feedback v2/2
Browse files
app.py
CHANGED
|
@@ -104,13 +104,29 @@ def get_files_by_marke(input_folder, marken):
|
|
| 104 |
return spalteninhalte
|
| 105 |
|
| 106 |
def get_asset_cell(file_path, filename, col_count):
|
| 107 |
-
"""Create optimized asset cell for PDF with
|
| 108 |
ext = Path(file_path).suffix.lower()
|
| 109 |
styles = getSampleStyleSheet()
|
| 110 |
|
| 111 |
try:
|
| 112 |
if ext in ALLOWED_IMAGE_EXTENSIONS:
|
| 113 |
with PILImage.open(file_path) as img:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
orig_width, orig_height = img.size
|
| 115 |
available_width = A4[0] - 40
|
| 116 |
max_width = available_width / col_count
|
|
@@ -125,8 +141,9 @@ def get_asset_cell(file_path, filename, col_count):
|
|
| 125 |
width = height * aspect_ratio
|
| 126 |
|
| 127 |
buffer = io.BytesIO()
|
| 128 |
-
img.
|
| 129 |
buffer.seek(0)
|
|
|
|
| 130 |
rl_img = RLImage(buffer, width=width, height=height)
|
| 131 |
|
| 132 |
frame = KeepInFrame(max_width, max_height + 30, content=[
|
|
@@ -139,6 +156,7 @@ def get_asset_cell(file_path, filename, col_count):
|
|
| 139 |
return Paragraph(f"{filename}", styles['Normal'])
|
| 140 |
except Exception as e:
|
| 141 |
return Paragraph(f"[Image Error]<br/>{filename}", styles['Normal'])
|
|
|
|
| 142 |
|
| 143 |
def build_marken_einzelseiten(alle_dateien_pro_marke, styles, gesamtbreite):
|
| 144 |
"""Build individual brand overview pages (original method)"""
|
|
@@ -468,7 +486,7 @@ def add_brand_pages_with_final_labels(elements, marken_spalten, renamed_files_by
|
|
| 468 |
elements.append(table)
|
| 469 |
|
| 470 |
def generate_two_section_pdf_report(input_folder, erste_marke=None):
|
| 471 |
-
"""Generate PDF report with two sections: by block and by brand"""
|
| 472 |
marken_set, renamed_files_by_folder_and_marke, all_files = analyze_files_by_filename(input_folder)
|
| 473 |
|
| 474 |
if not marken_set:
|
|
@@ -500,7 +518,15 @@ def generate_two_section_pdf_report(input_folder, erste_marke=None):
|
|
| 500 |
elements.append(Paragraph("<b>Section 1: Assets by Block</b>", styles['Title']))
|
| 501 |
elements.append(Spacer(1, 20))
|
| 502 |
|
|
|
|
| 503 |
for folder in sorted(renamed_files_by_folder_and_marke):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 504 |
elements.append(Paragraph(f"<b>Folder: {folder}</b>", styles['Heading2']))
|
| 505 |
|
| 506 |
abschnitt = renamed_files_by_folder_and_marke[folder]
|
|
@@ -519,35 +545,57 @@ def generate_two_section_pdf_report(input_folder, erste_marke=None):
|
|
| 519 |
col_data = []
|
| 520 |
max_rows = 0
|
| 521 |
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 536 |
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 551 |
elements.append(PageBreak())
|
| 552 |
|
| 553 |
# SECTION 2: BY BRAND
|
|
@@ -563,34 +611,45 @@ def generate_two_section_pdf_report(input_folder, erste_marke=None):
|
|
| 563 |
|
| 564 |
gesamt = 0
|
| 565 |
summary = []
|
|
|
|
| 566 |
for nummer, marke in marken_spalten:
|
| 567 |
count = global_counts[marke]
|
| 568 |
-
|
|
|
|
|
|
|
| 569 |
gesamt += count
|
| 570 |
summary.append(f"Total number of all assets: {gesamt}")
|
| 571 |
elements.append(Paragraph("<br/>".join(summary), styles['Normal']))
|
| 572 |
-
|
|
|
|
|
|
|
|
|
|
| 573 |
|
| 574 |
-
# Individual brand pages
|
| 575 |
-
for nummer, marke in
|
| 576 |
assets = []
|
| 577 |
for folder in renamed_files_by_folder_and_marke:
|
| 578 |
folder_assets = renamed_files_by_folder_and_marke[folder].get(marke, [])
|
| 579 |
brand_counter = 1 # Start counting from 1 for each brand in each folder
|
| 580 |
for pfad, original_name in folder_assets:
|
| 581 |
-
# Generate the ID name with sequential count
|
| 582 |
blocknummer = re.sub(r'\D', '', folder)[:2].zfill(2)
|
| 583 |
markennummer = marken_index[marke]
|
| 584 |
count_str = f"{brand_counter:02d}"
|
| 585 |
cleaned = get_cleaned_filename_without_brand(original_name, marke)
|
| 586 |
-
|
|
|
|
|
|
|
| 587 |
assets.append((pfad, id_name))
|
| 588 |
brand_counter += 1
|
| 589 |
|
| 590 |
-
if not assets:
|
| 591 |
continue
|
| 592 |
|
| 593 |
-
|
|
|
|
|
|
|
|
|
|
| 594 |
elements.append(Paragraph(f"<b>Brand Overview: {nummer} โ {marke}</b>", styles['Heading2']))
|
| 595 |
elements.append(Spacer(1, 6))
|
| 596 |
elements.append(Paragraph(f"Assets per Brand: {', '.join([f'{k}: {len([a for a in assets if extract_brand(Path(a[1]).stem) == k])}' for k in [marke]])}", styles['Normal']))
|
|
@@ -600,7 +659,8 @@ def generate_two_section_pdf_report(input_folder, erste_marke=None):
|
|
| 600 |
headers = ["Asset"] * 4
|
| 601 |
data = [headers]
|
| 602 |
row = []
|
| 603 |
-
for
|
|
|
|
| 604 |
cell = get_asset_cell(pfad, id_name, 4)
|
| 605 |
row.append(cell)
|
| 606 |
if len(row) == 4:
|
|
@@ -625,7 +685,6 @@ def generate_two_section_pdf_report(input_folder, erste_marke=None):
|
|
| 625 |
except Exception as e:
|
| 626 |
return None, f"Error generating PDF: {str(e)}"
|
| 627 |
|
| 628 |
-
|
| 629 |
def brand_renamer_tool():
|
| 630 |
st.header("Advanced Brand File Processor")
|
| 631 |
st.markdown("Automatically rename and organize brand assets with final names and outputs (pdf overview, excel output) for programming")
|
|
@@ -2387,7 +2446,7 @@ def find_smallest_dimensions_tool():
|
|
| 2387 |
|
| 2388 |
def resize_with_transparent_canvas_tool():
|
| 2389 |
st.header("Resize with Transparent Canvas")
|
| 2390 |
-
st.markdown("Resize images to
|
| 2391 |
|
| 2392 |
# File upload with unique key
|
| 2393 |
uploaded_file = st.file_uploader(
|
|
@@ -2398,35 +2457,8 @@ def resize_with_transparent_canvas_tool():
|
|
| 2398 |
)
|
| 2399 |
|
| 2400 |
if uploaded_file:
|
| 2401 |
-
#
|
| 2402 |
-
|
| 2403 |
-
with col1:
|
| 2404 |
-
mode = st.radio(
|
| 2405 |
-
"Resize mode:",
|
| 2406 |
-
["By height", "By width"],
|
| 2407 |
-
help="Resize all images to match either height or width while maintaining aspect ratio",
|
| 2408 |
-
key="resize_mode_selector"
|
| 2409 |
-
)
|
| 2410 |
-
with col2:
|
| 2411 |
-
# Set different limits based on mode
|
| 2412 |
-
if mode == "By height":
|
| 2413 |
-
target_value = st.number_input(
|
| 2414 |
-
"Target height (pixels):",
|
| 2415 |
-
min_value=10, # Minimum 10px height
|
| 2416 |
-
max_value=4000, # Maximum 4000px height
|
| 2417 |
-
value=1000, # Default 1000px
|
| 2418 |
-
step=10,
|
| 2419 |
-
key="target_height_input"
|
| 2420 |
-
)
|
| 2421 |
-
else: # By width
|
| 2422 |
-
target_value = st.number_input(
|
| 2423 |
-
"Target width (pixels):",
|
| 2424 |
-
min_value=10, # Minimum 10px width
|
| 2425 |
-
max_value=4000, # Maximum 4000px width
|
| 2426 |
-
value=1000, # Default 1000px
|
| 2427 |
-
step=10,
|
| 2428 |
-
key="target_width_input"
|
| 2429 |
-
)
|
| 2430 |
|
| 2431 |
# Process button with unique key
|
| 2432 |
if st.button("Process Images", type="primary", key="process_resize_transparent_images"):
|
|
@@ -2471,32 +2503,27 @@ def resize_with_transparent_canvas_tool():
|
|
| 2471 |
img = img.convert("RGBA")
|
| 2472 |
orig_w, orig_h = img.size
|
| 2473 |
|
| 2474 |
-
|
| 2475 |
-
|
| 2476 |
-
|
| 2477 |
-
|
| 2478 |
-
|
| 2479 |
-
|
| 2480 |
-
|
| 2481 |
-
|
| 2482 |
-
|
| 2483 |
-
|
|
|
|
|
|
|
| 2484 |
|
| 2485 |
# Resize image while maintaining aspect ratio
|
| 2486 |
img_resized = img.resize((new_w, new_h), PILImage.LANCZOS)
|
| 2487 |
|
| 2488 |
-
# Create transparent canvas with
|
| 2489 |
-
|
| 2490 |
-
# For height mode, canvas matches the resized width and target height
|
| 2491 |
-
canvas = PILImage.new("RGBA", (new_w, new_h), (0, 0, 0, 0))
|
| 2492 |
-
else:
|
| 2493 |
-
# For width mode, canvas matches the target width and resized height
|
| 2494 |
-
canvas = PILImage.new("RGBA", (new_w, new_h), (0, 0, 0, 0))
|
| 2495 |
|
| 2496 |
-
#
|
| 2497 |
-
|
| 2498 |
-
y_offset = (canvas.height - img_resized.height) // 2
|
| 2499 |
-
canvas.paste(img_resized, (x_offset, y_offset), mask=img_resized)
|
| 2500 |
|
| 2501 |
canvas.save(new_path)
|
| 2502 |
processed_count += 1
|
|
@@ -2512,6 +2539,7 @@ def resize_with_transparent_canvas_tool():
|
|
| 2512 |
Processing complete!
|
| 2513 |
- โ
Processed: {processed_count} images
|
| 2514 |
- โ Errors: {error_count} images
|
|
|
|
| 2515 |
""")
|
| 2516 |
|
| 2517 |
if processed_count > 0:
|
|
@@ -2530,12 +2558,12 @@ def resize_with_transparent_canvas_tool():
|
|
| 2530 |
st.download_button(
|
| 2531 |
label="๐ฅ Download Resized Images (ZIP)",
|
| 2532 |
data=zip_buffer.getvalue(),
|
| 2533 |
-
file_name="
|
| 2534 |
mime="application/zip",
|
| 2535 |
key="download_resized_transparent"
|
| 2536 |
)
|
| 2537 |
|
| 2538 |
-
# Show preview
|
| 2539 |
with st.expander("๐ผ๏ธ Preview Resized Images"):
|
| 2540 |
sample_files = []
|
| 2541 |
for root, dirs, files in os.walk(output_folder):
|
|
@@ -2548,7 +2576,7 @@ def resize_with_transparent_canvas_tool():
|
|
| 2548 |
for i, file_path in enumerate(sample_files):
|
| 2549 |
with cols[i % 3]:
|
| 2550 |
img = PILImage.open(file_path)
|
| 2551 |
-
st.image(img, caption=os.path.basename(file_path), use_column_width=True)
|
| 2552 |
|
| 2553 |
if processed_count > 6:
|
| 2554 |
st.info(f"Showing sample images. Total processed: {processed_count}")
|
|
@@ -2559,32 +2587,29 @@ def resize_with_transparent_canvas_tool():
|
|
| 2559 |
else:
|
| 2560 |
st.info("๐ Please upload a zip file with images to resize.")
|
| 2561 |
|
| 2562 |
-
# Instructions
|
| 2563 |
with st.expander("๐ Instructions"):
|
| 2564 |
st.markdown("""
|
| 2565 |
**How to use this tool:**
|
| 2566 |
|
| 2567 |
1. Upload a zip file containing images
|
| 2568 |
-
2.
|
| 2569 |
-
|
| 2570 |
-
- By width: All images will be resized to match the target width
|
| 2571 |
-
3. Set the target dimension (10-4000 pixels)
|
| 2572 |
4. Click "Process Images"
|
| 2573 |
5. Download the resized images with transparent backgrounds
|
| 2574 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2575 |
**Features:**
|
| 2576 |
- Maintains original aspect ratio
|
| 2577 |
-
- Centers images on transparent canvas
|
| 2578 |
- Outputs PNG files to preserve transparency
|
| 2579 |
- Preserves folder structure
|
| 2580 |
-
|
| 2581 |
-
**Limits:**
|
| 2582 |
-
- Minimum dimension: 10 pixels
|
| 2583 |
-
- Maximum dimension: 4000 pixels
|
| 2584 |
-
- For batch processing of many images, please be patient
|
| 2585 |
""")
|
| 2586 |
|
| 2587 |
-
|
| 2588 |
def center_on_canvas_tool():
|
| 2589 |
st.header("Center on Transparent Canvas")
|
| 2590 |
st.markdown("Center small images on a transparent 500x500 canvas.")
|
|
@@ -2809,13 +2834,33 @@ def generate_excel_report(output_folder: Path, marken_index: dict, file_to_facto
|
|
| 2809 |
|
| 2810 |
|
| 2811 |
def get_asset_cell(file_path, filename, col_count):
|
| 2812 |
-
"""Create optimized asset cell for PDF with
|
| 2813 |
ext = Path(file_path).suffix.lower()
|
| 2814 |
styles = getSampleStyleSheet()
|
| 2815 |
|
| 2816 |
try:
|
| 2817 |
if ext in ALLOWED_IMAGE_EXTENSIONS:
|
| 2818 |
with PILImage.open(file_path) as img:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2819 |
orig_width, orig_height = img.size
|
| 2820 |
available_width = A4[0] - 40
|
| 2821 |
max_width = available_width / col_count
|
|
@@ -2830,8 +2875,10 @@ def get_asset_cell(file_path, filename, col_count):
|
|
| 2830 |
width = height * aspect_ratio
|
| 2831 |
|
| 2832 |
buffer = io.BytesIO()
|
| 2833 |
-
|
|
|
|
| 2834 |
buffer.seek(0)
|
|
|
|
| 2835 |
rl_img = RLImage(buffer, width=width, height=height)
|
| 2836 |
|
| 2837 |
frame = KeepInFrame(max_width, max_height + 30, content=[
|
|
@@ -2844,6 +2891,7 @@ def get_asset_cell(file_path, filename, col_count):
|
|
| 2844 |
return Paragraph(f"{filename}", styles['Normal'])
|
| 2845 |
except Exception as e:
|
| 2846 |
return Paragraph(f"[Image Error]<br/>{filename}", styles['Normal'])
|
|
|
|
| 2847 |
|
| 2848 |
def analyze_files_by_filename(input_folder):
|
| 2849 |
"""Analyze files by extracting brand names from filenames"""
|
|
@@ -3480,33 +3528,31 @@ def add_brand_pages_with_png_extensions(elements, marken_spalten, renamed_files_
|
|
| 3480 |
def main():
|
| 3481 |
st.title("๐ง Brand Assets Tools")
|
| 3482 |
|
| 3483 |
-
tab1, tab2, tab3, tab4, tab5, tab6, tab7
|
| 3484 |
"Extract Images from Excel",
|
| 3485 |
"Name assets by brand",
|
|
|
|
| 3486 |
"Asset Overview",
|
| 3487 |
"Name stims by block + create output filesโ",
|
| 3488 |
-
"Find Smallest Dimensions",
|
| 3489 |
"Resize",
|
| 3490 |
-
"Place on Canvas"
|
| 3491 |
-
|
| 3492 |
])
|
| 3493 |
|
| 3494 |
with tab1:
|
| 3495 |
excel_extractor_tool()
|
| 3496 |
with tab2:
|
| 3497 |
-
file_renamer_tool()
|
| 3498 |
with tab3:
|
| 3499 |
-
|
| 3500 |
with tab4:
|
| 3501 |
-
|
| 3502 |
with tab5:
|
| 3503 |
-
|
| 3504 |
with tab6:
|
| 3505 |
resize_with_transparent_canvas_tool()
|
| 3506 |
with tab7:
|
| 3507 |
center_on_canvas_tool()
|
| 3508 |
-
with tab8:
|
| 3509 |
-
white_to_transparent_tool()
|
| 3510 |
|
| 3511 |
if __name__ == "__main__":
|
| 3512 |
main()
|
|
|
|
| 104 |
return spalteninhalte
|
| 105 |
|
| 106 |
def get_asset_cell(file_path, filename, col_count):
|
| 107 |
+
"""Create optimized asset cell for PDF with proper transparency handling"""
|
| 108 |
ext = Path(file_path).suffix.lower()
|
| 109 |
styles = getSampleStyleSheet()
|
| 110 |
|
| 111 |
try:
|
| 112 |
if ext in ALLOWED_IMAGE_EXTENSIONS:
|
| 113 |
with PILImage.open(file_path) as img:
|
| 114 |
+
# Preserve transparency if available
|
| 115 |
+
if img.mode in ('RGBA', 'LA') or (img.mode == 'P' and 'transparency' in img.info):
|
| 116 |
+
# Create a white background for the PDF (transparency doesn't work well in PDF)
|
| 117 |
+
background = PILImage.new('RGB', img.size, (255, 255, 255))
|
| 118 |
+
if img.mode == 'RGBA':
|
| 119 |
+
# Paste the image onto the white background using the alpha channel as mask
|
| 120 |
+
background.paste(img, mask=img.split()[-1])
|
| 121 |
+
else:
|
| 122 |
+
# For other transparent modes, convert to RGBA first
|
| 123 |
+
img_rgba = img.convert('RGBA')
|
| 124 |
+
background.paste(img_rgba, mask=img_rgba.split()[-1])
|
| 125 |
+
img = background.convert('RGB')
|
| 126 |
+
else:
|
| 127 |
+
# Convert non-transparent images to RGB
|
| 128 |
+
img = img.convert('RGB')
|
| 129 |
+
|
| 130 |
orig_width, orig_height = img.size
|
| 131 |
available_width = A4[0] - 40
|
| 132 |
max_width = available_width / col_count
|
|
|
|
| 141 |
width = height * aspect_ratio
|
| 142 |
|
| 143 |
buffer = io.BytesIO()
|
| 144 |
+
img.save(buffer, format='PNG')
|
| 145 |
buffer.seek(0)
|
| 146 |
+
|
| 147 |
rl_img = RLImage(buffer, width=width, height=height)
|
| 148 |
|
| 149 |
frame = KeepInFrame(max_width, max_height + 30, content=[
|
|
|
|
| 156 |
return Paragraph(f"{filename}", styles['Normal'])
|
| 157 |
except Exception as e:
|
| 158 |
return Paragraph(f"[Image Error]<br/>{filename}", styles['Normal'])
|
| 159 |
+
|
| 160 |
|
| 161 |
def build_marken_einzelseiten(alle_dateien_pro_marke, styles, gesamtbreite):
|
| 162 |
"""Build individual brand overview pages (original method)"""
|
|
|
|
| 486 |
elements.append(table)
|
| 487 |
|
| 488 |
def generate_two_section_pdf_report(input_folder, erste_marke=None):
|
| 489 |
+
"""Generate PDF report with two sections: by block and by brand - WITH EXTENSIONS - NO BLANK PAGES"""
|
| 490 |
marken_set, renamed_files_by_folder_and_marke, all_files = analyze_files_by_filename(input_folder)
|
| 491 |
|
| 492 |
if not marken_set:
|
|
|
|
| 518 |
elements.append(Paragraph("<b>Section 1: Assets by Block</b>", styles['Title']))
|
| 519 |
elements.append(Spacer(1, 20))
|
| 520 |
|
| 521 |
+
folders_with_content = []
|
| 522 |
for folder in sorted(renamed_files_by_folder_and_marke):
|
| 523 |
+
abschnitt = renamed_files_by_folder_and_marke[folder]
|
| 524 |
+
# Check if this folder has any content
|
| 525 |
+
total_files_in_folder = sum(len(abschnitt.get(marke, [])) for _, marke in marken_spalten)
|
| 526 |
+
if total_files_in_folder > 0:
|
| 527 |
+
folders_with_content.append(folder)
|
| 528 |
+
|
| 529 |
+
for i, folder in enumerate(folders_with_content):
|
| 530 |
elements.append(Paragraph(f"<b>Folder: {folder}</b>", styles['Heading2']))
|
| 531 |
|
| 532 |
abschnitt = renamed_files_by_folder_and_marke[folder]
|
|
|
|
| 545 |
col_data = []
|
| 546 |
max_rows = 0
|
| 547 |
|
| 548 |
+
# Only process brands that have content in this folder
|
| 549 |
+
brands_with_content = []
|
| 550 |
+
for nummer, marke in marken_spalten:
|
| 551 |
+
if len(abschnitt.get(marke, [])) > 0:
|
| 552 |
+
brands_with_content.append((nummer, marke))
|
| 553 |
+
|
| 554 |
+
if brands_with_content: # Only create table if there's content
|
| 555 |
+
headers = [f"{nummer} ({marke})" for nummer, marke in brands_with_content]
|
| 556 |
+
data = [headers]
|
| 557 |
+
col_data = []
|
| 558 |
+
max_rows = 0
|
| 559 |
+
|
| 560 |
+
for _, marke in brands_with_content:
|
| 561 |
+
eintraege = abschnitt.get(marke, [])
|
| 562 |
+
zellen = []
|
| 563 |
+
for p, original_name in eintraege:
|
| 564 |
+
# Generate the ID name - KEEP THE FILE EXTENSION
|
| 565 |
+
blocknummer = re.sub(r'\D', '', folder)[:2].zfill(2)
|
| 566 |
+
markennummer = marken_index[marke]
|
| 567 |
+
cleaned = get_cleaned_filename_without_brand(original_name, marke)
|
| 568 |
+
# Keep the original file extension
|
| 569 |
+
file_extension = Path(original_name).suffix
|
| 570 |
+
id_name = f"{markennummer}B{blocknummer}{marke}{cleaned}{file_extension}"
|
| 571 |
+
|
| 572 |
+
# Pass the full filename with extension to get_asset_cell
|
| 573 |
+
cell = get_asset_cell(p, id_name, len(headers))
|
| 574 |
+
zellen.append(cell)
|
| 575 |
+
col_data.append(zellen)
|
| 576 |
+
max_rows = max(max_rows, len(zellen))
|
| 577 |
|
| 578 |
+
for row_idx in range(max_rows):
|
| 579 |
+
row = []
|
| 580 |
+
for col in col_data:
|
| 581 |
+
row.append(col[row_idx] if row_idx < len(col) else "")
|
| 582 |
+
data.append(row)
|
| 583 |
|
| 584 |
+
col_width = (A4[0] - 40) / len(headers)
|
| 585 |
+
t = Table(data, colWidths=[col_width] * len(headers))
|
| 586 |
+
t.setStyle(TableStyle([
|
| 587 |
+
('GRID', (0, 0), (-1, -1), 0.5, colors.grey),
|
| 588 |
+
('BACKGROUND', (0, 0), (-1, 0), colors.lightgrey),
|
| 589 |
+
('VALIGN', (0, 0), (-1, -1), 'TOP'),
|
| 590 |
+
]))
|
| 591 |
+
elements.append(t)
|
| 592 |
+
|
| 593 |
+
# Only add page break if this is not the last folder with content
|
| 594 |
+
if i < len(folders_with_content) - 1:
|
| 595 |
+
elements.append(PageBreak())
|
| 596 |
+
|
| 597 |
+
# Add page break before Section 2 only if Section 1 had content
|
| 598 |
+
if folders_with_content:
|
| 599 |
elements.append(PageBreak())
|
| 600 |
|
| 601 |
# SECTION 2: BY BRAND
|
|
|
|
| 611 |
|
| 612 |
gesamt = 0
|
| 613 |
summary = []
|
| 614 |
+
brands_with_assets = [] # Track which brands actually have assets
|
| 615 |
for nummer, marke in marken_spalten:
|
| 616 |
count = global_counts[marke]
|
| 617 |
+
if count > 0: # Only include brands with assets
|
| 618 |
+
summary.append(f"{marke}: {count} assets")
|
| 619 |
+
brands_with_assets.append((nummer, marke))
|
| 620 |
gesamt += count
|
| 621 |
summary.append(f"Total number of all assets: {gesamt}")
|
| 622 |
elements.append(Paragraph("<br/>".join(summary), styles['Normal']))
|
| 623 |
+
|
| 624 |
+
# Only add page break if there are brands to show
|
| 625 |
+
if brands_with_assets:
|
| 626 |
+
elements.append(PageBreak())
|
| 627 |
|
| 628 |
+
# Individual brand pages - only for brands that have assets
|
| 629 |
+
for i, (nummer, marke) in enumerate(brands_with_assets):
|
| 630 |
assets = []
|
| 631 |
for folder in renamed_files_by_folder_and_marke:
|
| 632 |
folder_assets = renamed_files_by_folder_and_marke[folder].get(marke, [])
|
| 633 |
brand_counter = 1 # Start counting from 1 for each brand in each folder
|
| 634 |
for pfad, original_name in folder_assets:
|
| 635 |
+
# Generate the ID name with sequential count - KEEP THE FILE EXTENSION
|
| 636 |
blocknummer = re.sub(r'\D', '', folder)[:2].zfill(2)
|
| 637 |
markennummer = marken_index[marke]
|
| 638 |
count_str = f"{brand_counter:02d}"
|
| 639 |
cleaned = get_cleaned_filename_without_brand(original_name, marke)
|
| 640 |
+
# Keep the original file extension
|
| 641 |
+
file_extension = Path(original_name).suffix
|
| 642 |
+
id_name = f"{markennummer}B{blocknummer}{marke}{count_str}{cleaned}{file_extension}"
|
| 643 |
assets.append((pfad, id_name))
|
| 644 |
brand_counter += 1
|
| 645 |
|
| 646 |
+
if not assets: # Skip brands with no assets
|
| 647 |
continue
|
| 648 |
|
| 649 |
+
# Only add page break if this is not the first brand
|
| 650 |
+
if i > 0:
|
| 651 |
+
elements.append(PageBreak())
|
| 652 |
+
|
| 653 |
elements.append(Paragraph(f"<b>Brand Overview: {nummer} โ {marke}</b>", styles['Heading2']))
|
| 654 |
elements.append(Spacer(1, 6))
|
| 655 |
elements.append(Paragraph(f"Assets per Brand: {', '.join([f'{k}: {len([a for a in assets if extract_brand(Path(a[1]).stem) == k])}' for k in [marke]])}", styles['Normal']))
|
|
|
|
| 659 |
headers = ["Asset"] * 4
|
| 660 |
data = [headers]
|
| 661 |
row = []
|
| 662 |
+
for asset_idx, (pfad, id_name) in enumerate(assets):
|
| 663 |
+
# Pass the full filename with extension to get_asset_cell
|
| 664 |
cell = get_asset_cell(pfad, id_name, 4)
|
| 665 |
row.append(cell)
|
| 666 |
if len(row) == 4:
|
|
|
|
| 685 |
except Exception as e:
|
| 686 |
return None, f"Error generating PDF: {str(e)}"
|
| 687 |
|
|
|
|
| 688 |
def brand_renamer_tool():
|
| 689 |
st.header("Advanced Brand File Processor")
|
| 690 |
st.markdown("Automatically rename and organize brand assets with final names and outputs (pdf overview, excel output) for programming")
|
|
|
|
| 2446 |
|
| 2447 |
def resize_with_transparent_canvas_tool():
|
| 2448 |
st.header("Resize with Transparent Canvas")
|
| 2449 |
+
st.markdown("Resize images to 400px on the largest side while maintaining aspect ratio on transparent background.")
|
| 2450 |
|
| 2451 |
# File upload with unique key
|
| 2452 |
uploaded_file = st.file_uploader(
|
|
|
|
| 2457 |
)
|
| 2458 |
|
| 2459 |
if uploaded_file:
|
| 2460 |
+
# Show the fixed resize setting
|
| 2461 |
+
st.info("Images will be resized to 400px on the largest side (width or height), maintaining aspect ratio.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2462 |
|
| 2463 |
# Process button with unique key
|
| 2464 |
if st.button("Process Images", type="primary", key="process_resize_transparent_images"):
|
|
|
|
| 2503 |
img = img.convert("RGBA")
|
| 2504 |
orig_w, orig_h = img.size
|
| 2505 |
|
| 2506 |
+
# Calculate scaling factor based on largest side
|
| 2507 |
+
max_dimension = 400
|
| 2508 |
+
if orig_w >= orig_h:
|
| 2509 |
+
# Width is larger or equal
|
| 2510 |
+
scale_factor = max_dimension / orig_w
|
| 2511 |
+
new_w = max_dimension
|
| 2512 |
+
new_h = int(orig_h * scale_factor)
|
| 2513 |
+
else:
|
| 2514 |
+
# Height is larger
|
| 2515 |
+
scale_factor = max_dimension / orig_h
|
| 2516 |
+
new_h = max_dimension
|
| 2517 |
+
new_w = int(orig_w * scale_factor)
|
| 2518 |
|
| 2519 |
# Resize image while maintaining aspect ratio
|
| 2520 |
img_resized = img.resize((new_w, new_h), PILImage.LANCZOS)
|
| 2521 |
|
| 2522 |
+
# Create transparent canvas with the exact resized dimensions
|
| 2523 |
+
canvas = PILImage.new("RGBA", (new_w, new_h), (0, 0, 0, 0))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2524 |
|
| 2525 |
+
# The resized image fits perfectly on the canvas (no centering needed)
|
| 2526 |
+
canvas.paste(img_resized, (0, 0), mask=img_resized)
|
|
|
|
|
|
|
| 2527 |
|
| 2528 |
canvas.save(new_path)
|
| 2529 |
processed_count += 1
|
|
|
|
| 2539 |
Processing complete!
|
| 2540 |
- โ
Processed: {processed_count} images
|
| 2541 |
- โ Errors: {error_count} images
|
| 2542 |
+
- All images resized to 400px on largest side
|
| 2543 |
""")
|
| 2544 |
|
| 2545 |
if processed_count > 0:
|
|
|
|
| 2558 |
st.download_button(
|
| 2559 |
label="๐ฅ Download Resized Images (ZIP)",
|
| 2560 |
data=zip_buffer.getvalue(),
|
| 2561 |
+
file_name="resized_400px_images.zip",
|
| 2562 |
mime="application/zip",
|
| 2563 |
key="download_resized_transparent"
|
| 2564 |
)
|
| 2565 |
|
| 2566 |
+
# Show preview
|
| 2567 |
with st.expander("๐ผ๏ธ Preview Resized Images"):
|
| 2568 |
sample_files = []
|
| 2569 |
for root, dirs, files in os.walk(output_folder):
|
|
|
|
| 2576 |
for i, file_path in enumerate(sample_files):
|
| 2577 |
with cols[i % 3]:
|
| 2578 |
img = PILImage.open(file_path)
|
| 2579 |
+
st.image(img, caption=f"{os.path.basename(file_path)} ({img.size[0]}x{img.size[1]})", use_column_width=True)
|
| 2580 |
|
| 2581 |
if processed_count > 6:
|
| 2582 |
st.info(f"Showing sample images. Total processed: {processed_count}")
|
|
|
|
| 2587 |
else:
|
| 2588 |
st.info("๐ Please upload a zip file with images to resize.")
|
| 2589 |
|
| 2590 |
+
# Instructions
|
| 2591 |
with st.expander("๐ Instructions"):
|
| 2592 |
st.markdown("""
|
| 2593 |
**How to use this tool:**
|
| 2594 |
|
| 2595 |
1. Upload a zip file containing images
|
| 2596 |
+
2. All images will be resized to 400px on their largest side (width or height)
|
| 2597 |
+
3. Aspect ratio is maintained - smaller dimension will be scaled proportionally
|
|
|
|
|
|
|
| 2598 |
4. Click "Process Images"
|
| 2599 |
5. Download the resized images with transparent backgrounds
|
| 2600 |
|
| 2601 |
+
**Examples:**
|
| 2602 |
+
- 1000x800 image โ 400x320 image (width was larger)
|
| 2603 |
+
- 600x1200 image โ 200x400 image (height was larger)
|
| 2604 |
+
- 400x400 image โ 400x400 image (already correct size)
|
| 2605 |
+
|
| 2606 |
**Features:**
|
| 2607 |
- Maintains original aspect ratio
|
|
|
|
| 2608 |
- Outputs PNG files to preserve transparency
|
| 2609 |
- Preserves folder structure
|
| 2610 |
+
- Fixed 400px maximum on largest dimension
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2611 |
""")
|
| 2612 |
|
|
|
|
| 2613 |
def center_on_canvas_tool():
|
| 2614 |
st.header("Center on Transparent Canvas")
|
| 2615 |
st.markdown("Center small images on a transparent 500x500 canvas.")
|
|
|
|
| 2834 |
|
| 2835 |
|
| 2836 |
def get_asset_cell(file_path, filename, col_count):
|
| 2837 |
+
"""Create optimized asset cell for PDF with proper transparency handling"""
|
| 2838 |
ext = Path(file_path).suffix.lower()
|
| 2839 |
styles = getSampleStyleSheet()
|
| 2840 |
|
| 2841 |
try:
|
| 2842 |
if ext in ALLOWED_IMAGE_EXTENSIONS:
|
| 2843 |
with PILImage.open(file_path) as img:
|
| 2844 |
+
# Handle transparency properly
|
| 2845 |
+
if img.mode in ('RGBA', 'LA') or (img.mode == 'P' and 'transparency' in img.info):
|
| 2846 |
+
# Create a white background for transparent images
|
| 2847 |
+
background = PILImage.new('RGB', img.size, (255, 255, 255))
|
| 2848 |
+
if img.mode == 'RGBA':
|
| 2849 |
+
# Use alpha channel as mask
|
| 2850 |
+
background.paste(img, mask=img.split()[-1])
|
| 2851 |
+
elif img.mode == 'LA':
|
| 2852 |
+
# Luminance + Alpha
|
| 2853 |
+
img_rgba = img.convert('RGBA')
|
| 2854 |
+
background.paste(img_rgba, mask=img_rgba.split()[-1])
|
| 2855 |
+
elif img.mode == 'P' and 'transparency' in img.info:
|
| 2856 |
+
# Palette mode with transparency
|
| 2857 |
+
img_rgba = img.convert('RGBA')
|
| 2858 |
+
background.paste(img_rgba, mask=img_rgba.split()[-1])
|
| 2859 |
+
img = background
|
| 2860 |
+
else:
|
| 2861 |
+
# No transparency, safe to convert to RGB
|
| 2862 |
+
img = img.convert('RGB')
|
| 2863 |
+
|
| 2864 |
orig_width, orig_height = img.size
|
| 2865 |
available_width = A4[0] - 40
|
| 2866 |
max_width = available_width / col_count
|
|
|
|
| 2875 |
width = height * aspect_ratio
|
| 2876 |
|
| 2877 |
buffer = io.BytesIO()
|
| 2878 |
+
# Save as PNG to preserve quality
|
| 2879 |
+
img.save(buffer, format='PNG')
|
| 2880 |
buffer.seek(0)
|
| 2881 |
+
|
| 2882 |
rl_img = RLImage(buffer, width=width, height=height)
|
| 2883 |
|
| 2884 |
frame = KeepInFrame(max_width, max_height + 30, content=[
|
|
|
|
| 2891 |
return Paragraph(f"{filename}", styles['Normal'])
|
| 2892 |
except Exception as e:
|
| 2893 |
return Paragraph(f"[Image Error]<br/>{filename}", styles['Normal'])
|
| 2894 |
+
|
| 2895 |
|
| 2896 |
def analyze_files_by_filename(input_folder):
|
| 2897 |
"""Analyze files by extracting brand names from filenames"""
|
|
|
|
| 3528 |
def main():
|
| 3529 |
st.title("๐ง Brand Assets Tools")
|
| 3530 |
|
| 3531 |
+
tab1, tab2, tab3, tab4, tab5, tab6, tab7 = st.tabs([
|
| 3532 |
"Extract Images from Excel",
|
| 3533 |
"Name assets by brand",
|
| 3534 |
+
"Canvas white to Transparent",
|
| 3535 |
"Asset Overview",
|
| 3536 |
"Name stims by block + create output filesโ",
|
|
|
|
| 3537 |
"Resize",
|
| 3538 |
+
"Place on Canvas"
|
| 3539 |
+
|
| 3540 |
])
|
| 3541 |
|
| 3542 |
with tab1:
|
| 3543 |
excel_extractor_tool()
|
| 3544 |
with tab2:
|
| 3545 |
+
file_renamer_tool()
|
| 3546 |
with tab3:
|
| 3547 |
+
white_to_transparent_tool()
|
| 3548 |
with tab4:
|
| 3549 |
+
pdf_generator_tool()
|
| 3550 |
with tab5:
|
| 3551 |
+
brand_renamer_tool()
|
| 3552 |
with tab6:
|
| 3553 |
resize_with_transparent_canvas_tool()
|
| 3554 |
with tab7:
|
| 3555 |
center_on_canvas_tool()
|
|
|
|
|
|
|
| 3556 |
|
| 3557 |
if __name__ == "__main__":
|
| 3558 |
main()
|