|
import os |
|
import glob |
|
import multiprocessing |
|
from PIL import Image |
|
|
|
|
|
Image.MAX_IMAGE_PIXELS = 139211472 |
|
|
|
|
|
def convert_rgba_to_rgb(image_path): |
|
""" |
|
Convert an RGBA image to RGB format. |
|
""" |
|
try: |
|
print(f"Opening image: {image_path}") |
|
with Image.open(image_path) as image: |
|
print(f"Image mode: {image.mode}") |
|
if image.mode == "RGBA": |
|
rgb_image = image.convert("RGB") |
|
rgb_image.save(image_path) |
|
print(f"Converted {image_path} to RGB.") |
|
else: |
|
print(f"{image_path} is not an RGBA image.") |
|
except Exception as e: |
|
print(f"Error processing {image_path}: {e}") |
|
|
|
|
|
def main(): |
|
""" |
|
Main function to convert all RGBA images to RGB in a directory. |
|
""" |
|
directory = r"E:\training_dir" |
|
print(f"Directory set to: {directory}") |
|
|
|
|
|
files = glob.glob(os.path.join(directory, "**", "*.png"), recursive=True) |
|
print(f"Found {len(files)} .png files in directory and subdirectories.") |
|
|
|
|
|
num_processes = multiprocessing.cpu_count() |
|
print(f"Number of processes set to the number of available CPUs: {num_processes}") |
|
|
|
|
|
with multiprocessing.Pool(num_processes) as pool: |
|
print("Pool of processes created.") |
|
|
|
pool.map(convert_rgba_to_rgb, files) |
|
|
|
print("Conversion complete.") |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |
|
|