Upload FileConverter.py
Browse files- FileConverter.py +28 -0
FileConverter.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
def batch_convert_images(input_dir, output_dir):
|
| 5 |
+
# Create the output directory if it doesn't exist
|
| 6 |
+
if not os.path.exists(output_dir):
|
| 7 |
+
os.makedirs(output_dir)
|
| 8 |
+
|
| 9 |
+
# Process each file in the input directory
|
| 10 |
+
for filename in os.listdir(input_dir):
|
| 11 |
+
# Check for .ppm or .pgm files (case-insensitive)
|
| 12 |
+
if filename.lower().endswith('.ppm') or filename.lower().endswith('.pgm'):
|
| 13 |
+
img_path = os.path.join(input_dir, filename)
|
| 14 |
+
img = Image.open(img_path)
|
| 15 |
+
|
| 16 |
+
# Create the new filename with .png extension
|
| 17 |
+
new_filename = os.path.splitext(filename)[0] + '.png'
|
| 18 |
+
save_path = os.path.join(output_dir, new_filename)
|
| 19 |
+
|
| 20 |
+
# Save the image as .png
|
| 21 |
+
img.save(save_path)
|
| 22 |
+
print(f"Converted: {filename} -> {new_filename}")
|
| 23 |
+
|
| 24 |
+
# Example usage
|
| 25 |
+
input_dir = 'D:/U2UData-2/Sunny_Rain/2025-07-28-20-19-32/images'
|
| 26 |
+
output_dir = 'D:/U2UData-2/Sunny_Rain/2025-07-28-20-19-32/PNG'
|
| 27 |
+
print("------------------")
|
| 28 |
+
batch_convert_images(input_dir, output_dir)
|