File size: 1,463 Bytes
35355b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
912bdff
d7eb5d6
 
 
 
912bdff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
---
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-*
---



![image/jpeg](https://cdn-uploads.huggingface.co/production/uploads/634dffc49b777beec3bc6448/OrXoOBspR7eazsxCja-wx.jpeg)

```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")
```