File size: 1,128 Bytes
202fcb6
 
 
 
 
 
 
 
3a5662d
 
 
202fcb6
3a5662d
202fcb6
3a5662d
 
 
202fcb6
 
 
 
 
 
 
3a5662d
 
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
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, (image_base64, caption) in enumerate(zip(df['image'], df['caption'])):
        # Decode and save the image
        image_data = base64.b64decode(image_base64)
        image = Image.open(io.BytesIO(image_data))
        image.save(os.path.join(output_dir, f"image_{i}.png"))

        # Save the caption
        with open(os.path.join(output_dir, f"caption_{i}.txt"), 'w') as file:
            file.write(caption)

        print(f"Saved Image and Caption {i}")

def main():
    # Load dataset from Hugging Face
    dataset = load_dataset("dataautogpt3/Dalle3")

    # Assuming the first split contains the data
    df = pd.DataFrame(dataset[next(iter(dataset))])

    # 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()