svjack commited on
Commit
912bdff
·
verified ·
1 Parent(s): 35355b5

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +36 -0
README.md CHANGED
@@ -15,3 +15,39 @@ configs:
15
  - split: train
16
  path: data/train-*
17
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  - split: train
16
  path: data/train-*
17
  ---
18
+
19
+ ```python
20
+ from datasets import load_dataset
21
+ from PIL import Image, ImageOps
22
+
23
+ def resize_and_pad(image, target_size=(512, 512)):
24
+ # 计算原始图像的宽高比
25
+ width, height = image.size
26
+ target_width, target_height = target_size
27
+ ratio = min(target_width / width, target_height / height)
28
+
29
+ # 等比例缩放图像
30
+ new_size = (int(width * ratio), int(height * ratio))
31
+ resized_image = image.resize(new_size)
32
+
33
+ # 创建一个新的黑色背景图像
34
+ new_image = Image.new("RGB", target_size, (0, 0, 0))
35
+
36
+ # 将缩放后的图像粘贴到新图像的中心
37
+ new_image.paste(resized_image, ((target_width - new_size[0]) // 2, (target_height - new_size[1]) // 2))
38
+
39
+ return new_image
40
+
41
+ # 加载数据集
42
+ ds = load_dataset("svjack/Prince_Star")
43
+
44
+ # 对数据集中的 image 列进行处理
45
+ def process_example(example):
46
+ example['image'] = resize_and_pad(example['image'])
47
+ return example
48
+
49
+ # 应用处理函数到整个数据集
50
+ ds = ds.map(process_example)
51
+
52
+ ds.push_to_hub("svjack/Prince_Star_512x512")
53
+ ```