import base64 from PIL import Image import io import os import pandas as pd from datasets import load_dataset def decode_and_save_images(df, output_dir): for i, row in df.iterrows(): # Decode image image_data = base64.b64decode(row['image']) image = Image.open(io.BytesIO(image_data)) # Save image image_filename = f"{output_dir}/image_{i}.png" image.save(image_filename) # Save caption caption_filename = f"{output_dir}/caption_{i}.txt" with open(caption_filename, 'w') as file: file.write(row['caption']) print(f"Saved Image and Caption {i}") def main(): # Load dataset from Hugging Face dataset = load_dataset("dataautogpt3/Dalle3") # Convert to Pandas DataFrame (assuming the dataset is in the first split) df = pd.DataFrame(dataset['train']) # Specify your desired output directory here output_dir = '/path/to/your/desired/output' # Replace with your specific path os.makedirs(output_dir, exist_ok=True) # Process and save images and captions decode_and_save_images(df, output_dir) if __name__ == "__main__": main()