Gnanashri Kota Venkatesh commited on
Commit
3ed5aab
·
unverified ·
1 Parent(s): f16a266

Update tab6.py

Browse files

To Solve the extension issue

Files changed (1) hide show
  1. tab6.py +43 -33
tab6.py CHANGED
@@ -1,6 +1,7 @@
1
  import io
2
  import os
3
  import zipfile
 
4
  import streamlit as st
5
  from PIL import Image as PILImage
6
 
@@ -53,9 +54,8 @@ def resize_with_transparent_canvas_tool():
53
  all_files = []
54
  for root, dirs, files in os.walk(input_folder):
55
  for file in files:
56
- ext = os.path.splitext(file)[1].lower()
57
- if ext in ALLOWED_IMAGE_EXTENSIONS:
58
- all_files.append(os.path.join(root, file))
59
 
60
  total_files = len(all_files)
61
 
@@ -65,37 +65,47 @@ def resize_with_transparent_canvas_tool():
65
 
66
  try:
67
  relative_path = os.path.relpath(file_path, input_folder)
68
- new_path = os.path.join(output_folder, os.path.splitext(relative_path)[0] + ".png")
69
- os.makedirs(os.path.dirname(new_path), exist_ok=True)
70
 
71
- with PILImage.open(file_path) as img:
72
- img = img.convert("RGBA")
73
- orig_w, orig_h = img.size
74
-
75
- # Calculate scaling factor based on largest side
76
- max_dimension = 400
77
- if orig_w >= orig_h:
78
- # Width is larger or equal
79
- scale_factor = max_dimension / orig_w
80
- new_w = max_dimension
81
- new_h = int(orig_h * scale_factor)
82
- else:
83
- # Height is larger
84
- scale_factor = max_dimension / orig_h
85
- new_h = max_dimension
86
- new_w = int(orig_w * scale_factor)
87
-
88
- # Resize image while maintaining aspect ratio
89
- img_resized = img.resize((new_w, new_h), PILImage.LANCZOS)
90
-
91
- # Create transparent canvas with the exact resized dimensions
92
- canvas = PILImage.new("RGBA", (new_w, new_h), (0, 0, 0, 0))
93
-
94
- # The resized image fits perfectly on the canvas (no centering needed)
95
- canvas.paste(img_resized, (0, 0), mask=img_resized)
96
 
97
- canvas.save(new_path)
98
- processed_count += 1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  except Exception as e:
100
  error_count += 1
101
  st.warning(f"Error processing {os.path.basename(file_path)}: {str(e)}")
@@ -177,4 +187,4 @@ def resize_with_transparent_canvas_tool():
177
  - Outputs PNG files to preserve transparency
178
  - Preserves folder structure
179
  - Fixed 400px maximum on largest dimension
180
- """)
 
1
  import io
2
  import os
3
  import zipfile
4
+ import shutil
5
  import streamlit as st
6
  from PIL import Image as PILImage
7
 
 
54
  all_files = []
55
  for root, dirs, files in os.walk(input_folder):
56
  for file in files:
57
+ # Accept ALL file extensions, not just images
58
+ all_files.append(os.path.join(root, file))
 
59
 
60
  total_files = len(all_files)
61
 
 
65
 
66
  try:
67
  relative_path = os.path.relpath(file_path, input_folder)
68
+ ext = os.path.splitext(file_path)[1].lower()
 
69
 
70
+ # Only process if it's an image, otherwise copy as-is
71
+ if ext in ALLOWED_IMAGE_EXTENSIONS:
72
+ new_path = os.path.join(output_folder, os.path.splitext(relative_path)[0] + ".png")
73
+ os.makedirs(os.path.dirname(new_path), exist_ok=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
+ with PILImage.open(file_path) as img:
76
+ img = img.convert("RGBA")
77
+ orig_w, orig_h = img.size
78
+
79
+ # Calculate scaling factor based on largest side
80
+ max_dimension = 400
81
+ if orig_w >= orig_h:
82
+ # Width is larger or equal
83
+ scale_factor = max_dimension / orig_w
84
+ new_w = max_dimension
85
+ new_h = int(orig_h * scale_factor)
86
+ else:
87
+ # Height is larger
88
+ scale_factor = max_dimension / orig_h
89
+ new_h = max_dimension
90
+ new_w = int(orig_w * scale_factor)
91
+
92
+ # Resize image while maintaining aspect ratio
93
+ img_resized = img.resize((new_w, new_h), PILImage.LANCZOS)
94
+
95
+ # Create transparent canvas with the exact resized dimensions
96
+ canvas = PILImage.new("RGBA", (new_w, new_h), (0, 0, 0, 0))
97
+
98
+ # The resized image fits perfectly on the canvas (no centering needed)
99
+ canvas.paste(img_resized, (0, 0), mask=img_resized)
100
+
101
+ canvas.save(new_path)
102
+ else:
103
+ # Not an image - copy as-is with original extension
104
+ new_path = os.path.join(output_folder, relative_path)
105
+ os.makedirs(os.path.dirname(new_path), exist_ok=True)
106
+ shutil.copy2(file_path, new_path)
107
+
108
+ processed_count += 1
109
  except Exception as e:
110
  error_count += 1
111
  st.warning(f"Error processing {os.path.basename(file_path)}: {str(e)}")
 
187
  - Outputs PNG files to preserve transparency
188
  - Preserves folder structure
189
  - Fixed 400px maximum on largest dimension
190
+ """)