import os
from PIL import Image

def convert_all_png_to_jpg(input_folder, output_folder, quality=85):
    # Create the output folder if it doesn't exist
    os.makedirs(output_folder, exist_ok=True)

    # Iterate over all files in the input folder
    for filename in os.listdir(input_folder):
        if filename.lower().endswith('.png'):
            # Define full file path
            png_file_path = os.path.join(input_folder, filename)
            
            # Open the .png image file
            with Image.open(png_file_path) as img:
                # Define the new file path with .jpg extension
                jpg_file_path = os.path.join(output_folder, os.path.splitext(filename)[0] + '.jpg')

                # Convert the image to RGB mode
                rgb_img = img.convert('RGB')

                # Save the image in .jpg format with specified quality
                rgb_img.save(jpg_file_path, 'JPEG', quality=quality)

                print(f"Converted: {png_file_path} to {jpg_file_path} with quality {quality}")

# Example usage
input_folder = '/home/worker/yt/autocon/dataset/site1_column/images'
output_folder = '/home/worker/yt/autocon/dataset/site1_column/jpg_images'
convert_all_png_to_jpg(input_folder, output_folder, quality=800)
