Dataset Viewer
The dataset viewer is not available for this dataset.
Unexpected token '<', "<html> <h"... is not valid JSON

Need help to make the dataset viewer work? Make sure to review how to configure the dataset viewer, and open a discussion for direct support.

from datasets import load_dataset
from PIL import Image
import numpy as np
import os
from tqdm import tqdm

# 加载数据集
dataset_path = "path_to/style_fonts_img"
dataset = load_dataset(dataset_path)

# 创建保存目录
save_dir = os.path.join(dataset_path, "extracted_images")
os.makedirs(save_dir, exist_ok=True)

# 获取数据集大小
total_samples = len(dataset['train'])
print(f"数据集共有 {total_samples} 个样本")

# 使用tqdm创建进度条
for i, example in tqdm(enumerate(dataset['train']), total=total_samples, desc="处理图像"):
    try:
        # 获取图像数据
        image_array = example['image']
        
        # 转换为PIL图像
        image = Image.fromarray(np.uint8(image_array))
        
        # 获取文件名
        filename = example['filename'] 
        
        # 保存图像
        image_path = os.path.join(save_dir, filename)
        image.save(image_path)
        
        # 保存文本
        if 'text' in example and example['text']:
            text_filename = os.path.splitext(filename)[0] + '.txt'
            text_path = os.path.join(text_dir, text_filename)
            with open(text_path, 'w', encoding='utf-8') as f:
                f.write(example['text'])
        
        # print(f"已保存 {filename}")
        
        # 只处理前10个样本(可选)
        if i >= 9:
            break
            
    except Exception as e:
        print(f"处理样本 {i} 时出错: {e}")

print("处理完成!")
Downloads last month
103