Commit
·
202fcb6
1
Parent(s):
11e438e
Create download.py
Browse files- download.py +40 -0
download.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import io
|
| 4 |
+
import os
|
| 5 |
+
import pandas as pd
|
| 6 |
+
from datasets import load_dataset
|
| 7 |
+
|
| 8 |
+
def decode_and_save_images(df, output_dir):
|
| 9 |
+
for i, row in df.iterrows():
|
| 10 |
+
# Decode image
|
| 11 |
+
image_data = base64.b64decode(row['image'])
|
| 12 |
+
image = Image.open(io.BytesIO(image_data))
|
| 13 |
+
|
| 14 |
+
# Save image
|
| 15 |
+
image_filename = f"{output_dir}/image_{i}.png"
|
| 16 |
+
image.save(image_filename)
|
| 17 |
+
|
| 18 |
+
# Save caption
|
| 19 |
+
caption_filename = f"{output_dir}/caption_{i}.txt"
|
| 20 |
+
with open(caption_filename, 'w') as file:
|
| 21 |
+
file.write(row['caption'])
|
| 22 |
+
|
| 23 |
+
print(f"Saved Image and Caption {i}")
|
| 24 |
+
|
| 25 |
+
def main():
|
| 26 |
+
# Load dataset from Hugging Face
|
| 27 |
+
dataset = load_dataset("dataautogpt3/Dalle3")
|
| 28 |
+
|
| 29 |
+
# Convert to Pandas DataFrame (assuming the dataset is in the first split)
|
| 30 |
+
df = pd.DataFrame(dataset['train'])
|
| 31 |
+
|
| 32 |
+
# Specify your desired output directory here
|
| 33 |
+
output_dir = '/path/to/your/desired/output' # Replace with your specific path
|
| 34 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 35 |
+
|
| 36 |
+
# Process and save images and captions
|
| 37 |
+
decode_and_save_images(df, output_dir)
|
| 38 |
+
|
| 39 |
+
if __name__ == "__main__":
|
| 40 |
+
main()
|