mercurystraw
commited on
Commit
·
075deb7
1
Parent(s):
ebde6d8
test
Browse files
lena.png
ADDED
|
Git LFS Details
|
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")
|