File size: 866 Bytes
075deb7 |
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 |
from PIL import Image
import numpy as np
import pandas as pd
from datasets import Dataset
import pyarrow as pa
import pyarrow.parquet as pq
# 读取图片并转换为 numpy 数组
img_path = "lena.png"
img = Image.open(img_path)
img_array = np.array(img)
# 将图像数组转换为 DataFrame 格式
df = pd.DataFrame(img_array.reshape(-1, img_array.shape[2]), columns=[f'pixel_{i}' for i in range(img_array.shape[2])])
# 插入前几行为空的行(例如前5行)
num_empty_rows = 5
empty_df = pd.DataFrame([[None]*df.shape[1]] * num_empty_rows, columns=df.columns)
df = pd.concat([empty_df, df], ignore_index=True)
# 将 DataFrame 转换为 Dataset 格式
dataset = Dataset.from_pandas(df)
# 将 Dataset 转换为 Parquet 格式并保存
table = pa.Table.from_pandas(df)
pq.write_table(table, 'lena.parquet')
print("Parquet 文件已生成:lena.parquet")
|