|
|
--- |
|
|
dataset_info: |
|
|
features: |
|
|
- name: image |
|
|
dtype: image |
|
|
splits: |
|
|
- name: train |
|
|
num_bytes: 1417616.0 |
|
|
num_examples: 35 |
|
|
download_size: 1415836 |
|
|
dataset_size: 1417616.0 |
|
|
configs: |
|
|
- config_name: default |
|
|
data_files: |
|
|
- split: train |
|
|
path: data/train-* |
|
|
--- |
|
|
|
|
|
|
|
|
|
|
|
 |
|
|
|
|
|
```python |
|
|
from datasets import load_dataset |
|
|
from PIL import Image, ImageOps |
|
|
|
|
|
def resize_and_pad(image, target_size=(512, 512)): |
|
|
# 计算原始图像的宽高比 |
|
|
width, height = image.size |
|
|
target_width, target_height = target_size |
|
|
ratio = min(target_width / width, target_height / height) |
|
|
|
|
|
# 等比例缩放图像 |
|
|
new_size = (int(width * ratio), int(height * ratio)) |
|
|
resized_image = image.resize(new_size) |
|
|
|
|
|
# 创建一个新的黑色背景图像 |
|
|
new_image = Image.new("RGB", target_size, (0, 0, 0)) |
|
|
|
|
|
# 将缩放后的图像粘贴到新图像的中心 |
|
|
new_image.paste(resized_image, ((target_width - new_size[0]) // 2, (target_height - new_size[1]) // 2)) |
|
|
|
|
|
return new_image |
|
|
|
|
|
# 加载数据集 |
|
|
ds = load_dataset("svjack/Prince_Star") |
|
|
|
|
|
# 对数据集中的 image 列进行处理 |
|
|
def process_example(example): |
|
|
example['image'] = resize_and_pad(example['image']) |
|
|
return example |
|
|
|
|
|
# 应用处理函数到整个数据集 |
|
|
ds = ds.map(process_example) |
|
|
|
|
|
ds.push_to_hub("svjack/Prince_Star_512x512") |
|
|
``` |