如何在Python中将1D像素数组转换为图像?

问题描述 投票:0回答:1

这是关于FER2013数据集(https://www.kaggle.com/c/challenges-in-representation-learning-facial-expression-recognition-challenge/data).The数据由面部的48x48像素灰度图像组成.CSV文件包含三列(情感,像素,用法),其中Usage具有三个值 - 训练,PrivateTest和PublicTest中的任何一个.I想要读取像素数组,将它们转换为图像并将它们保存在按其使用类型命名的相应文件夹中。

我需要可以执行上述操作的python代码。以下是我的代码

import pandas as pd
import numpy as np
from PIL import Image

df=pd.read_csv("fer2013.csv")
for rows in df:

   arr=np.array(df['pixels'])
   print(arr)
   print(arr.shape)
   img = Image.fromarray(arr.reshape(48,48), 'L')
   img.save("dataset/df['Usage']/img.jpg", "JPEG")

上面的代码显示错误:无法将大小为35887的数组重塑为形状(48,48)。

提前致谢。

python image detection emotion
1个回答
0
投票

如果有任何疑问(因为我一直在使用FER数据集):

import pandas as pd
import numpy as np
from PIL import Image

df = pd.read_csv('fer2013.csv')
for image_pixels in df.iloc[1:,1]: #column 2 has the pixels. Row 1 is column name.
    image_string = image_pixels.split(' ') #pixels are separated by spaces.
    image_data = np.asarray(image_string, dtype=np.uint8).reshape(48,48)
    img = Image.fromarray(image_data) #final image
© www.soinside.com 2019 - 2024. All rights reserved.