mercurystraw commited on
Commit
075deb7
·
1 Parent(s): ebde6d8
Files changed (2) hide show
  1. lena.png +3 -0
  2. test.py +26 -0
lena.png ADDED

Git LFS Details

  • SHA256: 147c33e1ee3a358eebae73ee8c16306984321f1c5e483c2c4221738f7b6ccc8f
  • Pointer size: 131 Bytes
  • Size of remote file: 274 kB
test.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+ import numpy as np
3
+ import pandas as pd
4
+ from datasets import Dataset
5
+ import pyarrow as pa
6
+ import pyarrow.parquet as pq
7
+
8
+ # 读取图片并转换为 numpy 数组
9
+ img_path = "lena.png"
10
+ img = Image.open(img_path)
11
+ img_array = np.array(img)
12
+
13
+ # 将图像数组转换为 DataFrame 格式
14
+ df = pd.DataFrame(img_array.reshape(-1, img_array.shape[2]), columns=[f'pixel_{i}' for i in range(img_array.shape[2])])
15
+ # 插入前几行为空的行(例如前5行)
16
+ num_empty_rows = 5
17
+ empty_df = pd.DataFrame([[None]*df.shape[1]] * num_empty_rows, columns=df.columns)
18
+ df = pd.concat([empty_df, df], ignore_index=True)
19
+ # 将 DataFrame 转换为 Dataset 格式
20
+ dataset = Dataset.from_pandas(df)
21
+
22
+ # 将 Dataset 转换为 Parquet 格式并保存
23
+ table = pa.Table.from_pandas(df)
24
+ pq.write_table(table, 'lena.parquet')
25
+
26
+ print("Parquet 文件已生成:lena.parquet")