File size: 1,167 Bytes
202fcb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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()