feat: update generation script
Browse files- assets/Aurebesh +0 -0
- bin/generate.py +58 -0
assets/Aurebesh
ADDED
File without changes
|
bin/generate.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
import re
|
4 |
+
|
5 |
+
from datasets import load_dataset
|
6 |
+
from PIL import Image, ImageDraw, ImageFont
|
7 |
+
|
8 |
+
FONT_PATH = "assets/Aurebesh"
|
9 |
+
FONT_SIZE = 50
|
10 |
+
IMAGE_SIZE = (784, 784)
|
11 |
+
OUTPUT_DIR = "images"
|
12 |
+
|
13 |
+
font = ImageFont.truetype(FONT_PATH, FONT_SIZE)
|
14 |
+
|
15 |
+
##### Get all words from Paul Graham Essays #####
|
16 |
+
paul_graham_ds = load_dataset("sgoel9/paul_graham_essays", split="train")
|
17 |
+
all_words = []
|
18 |
+
|
19 |
+
for entry in paul_graham_ds:
|
20 |
+
text = str(entry["text"]) # pyright: ignore
|
21 |
+
words = re.findall(r"\b\w+\b", text.lower())
|
22 |
+
all_words.extend(words)
|
23 |
+
|
24 |
+
|
25 |
+
def generate_aurebesh_image(text, output_dir):
|
26 |
+
# TODO: Generate images with various background colours
|
27 |
+
img = Image.new("RGB", IMAGE_SIZE, color=(255, 255, 255))
|
28 |
+
d = ImageDraw.Draw(img)
|
29 |
+
|
30 |
+
# TODO: Experiment with various orientations
|
31 |
+
bbox = d.textbbox((0, 0), text, font=font)
|
32 |
+
# atm, text is rendered in the center
|
33 |
+
text_width, text_height = bbox[2] - bbox[0], bbox[3] - bbox[1]
|
34 |
+
position = ((IMAGE_SIZE[0] - text_width) // 2, (IMAGE_SIZE[1] - text_height) // 2)
|
35 |
+
|
36 |
+
# Add the text to the image
|
37 |
+
d.text(position, text, font=font, fill=(0, 0, 0))
|
38 |
+
|
39 |
+
# Save the image
|
40 |
+
if not os.path.exists(output_dir):
|
41 |
+
os.makedirs(output_dir)
|
42 |
+
img.save(os.path.join(output_dir, f"{text}.png"))
|
43 |
+
|
44 |
+
|
45 |
+
captions = []
|
46 |
+
for word in all_words:
|
47 |
+
clean_word = re.sub(r"\W+", "", word)
|
48 |
+
|
49 |
+
generate_aurebesh_image(clean_word, OUTPUT_DIR)
|
50 |
+
|
51 |
+
captions.append({"file_name": f"{clean_word}.png", "text": word})
|
52 |
+
|
53 |
+
with open(os.path.join(OUTPUT_DIR, "metadata.jsonl"), "w") as f:
|
54 |
+
for item in captions:
|
55 |
+
f.write(json.dumps(item) + "\n")
|
56 |
+
|
57 |
+
dataset = load_dataset("imagefolder", data_dir=OUTPUT_DIR, split="all")
|
58 |
+
dataset.push_to_hub("SauravMaheshkar/aurebesh") # pyright: ignore
|