利用jupyter实现程序时报错,显示UnidentifiedImageErrorTraceback (most recent call last) Cell I
  kIM7GUNpnV3x 2023年12月06日 55 0

一:具体描述

具体程序代码:

import numpy as np
import pandas as pd
import os, sys
from sklearn.decomposition import PCA
from PIL import Image, ImageShow

# 数据集
dataSetsPath = "D:/Download/orl_faces/"
data_dict = {"dirName": [], "dirPath":[], "imgArr":[]}
for ix, file in enumerate(os.walk(dataSetsPath)):
        dir_path, dir_name, content_name = file
        file_name = dir_path.split("/")[-1]
        if 's'in file_name:
            for content in content_name:
                file_dir = dir_path + "/" + content
                tmp_image = Image.open(file_dir)
                img_arr = np.asarray(tmp_image).reshape(1, -1).astype('float').tolist()[0]
                # 存储
                data_dict['dirName'].append(file_name)
                data_dict['dirPath'].append(file_dir.split("/")[-1])
                data_dict['imgArr'].append(img_arr)
                
# dict to DataFrame
data_df = pd.DataFrame(data_dict)
data_df.query("dirName in ['s1']")
# print(data_df.head(5))


错误是这样的

UnidentifiedImageErrorTraceback (most recent call last)
Cell In[30], line 16
     14 for content in content_name:
     15     file_dir = dir_path + "/" + content
---> 16     tmp_image = Image.open(file_dir)
     17     img_arr = np.asarray(tmp_image).reshape(1, -1).astype('float').tolist()[0]
     18     # 存储

File D:\ProgramData\anaconda3\lib\site-packages\PIL\Image.py:3283, in open(fp, mode, formats)
   3281     warnings.warn(message)
   3282 msg = "cannot identify image file %r" % (filename if filename else fp)
-> 3283 raise UnidentifiedImageError(msg)

UnidentifiedImageError: cannot identify image file 'D:/Download/orl_faces/orl_faces/eigen.txt'

需要读取文件如下图所示:

这是一个压缩包,为了实现需求,所以需要先解压

                   利用jupyter实现程序时报错,显示UnidentifiedImageErrorTraceback (most recent call last) Cell I_Image

解压的截图如下:

                   利用jupyter实现程序时报错,显示UnidentifiedImageErrorTraceback (most recent call last) Cell I_Image_02


代码中的路径是这样的,

                   利用jupyter实现程序时报错,显示UnidentifiedImageErrorTraceback (most recent call last) Cell I_Image_03

以为是对的,寻找了报错处,发现代码一点也没错。问题就是在路径上

                   利用jupyter实现程序时报错,显示UnidentifiedImageErrorTraceback (most recent call last) Cell I_ide_04

最后打开解压文件,在解压文件中可以看出

                   利用jupyter实现程序时报错,显示UnidentifiedImageErrorTraceback (most recent call last) Cell I_Image_05

                   利用jupyter实现程序时报错,显示UnidentifiedImageErrorTraceback (most recent call last) Cell I_ide_06

最后发现解压的文件夹下还有一层吗,最后将代码改了一下,然后就运行成功了

修改后的代码如下:

import numpy as np
import pandas as pd
import os, sys
from sklearn.decomposition import PCA
from PIL import Image, ImageShow

# 数据集
dataSetsPath = "D:/Download/orl_faces/orl_faces/"
data_dict = {"dirName": [], "dirPath":[], "imgArr":[]}
for ix, file in enumerate(os.walk(dataSetsPath)):
        dir_path, dir_name, content_name = file
        file_name = dir_path.split("/")[-1]
        if 's'in file_name:
            for content in content_name:
                file_dir = dir_path + "/" + content
                tmp_image = Image.open(file_dir)
                img_arr = np.asarray(tmp_image).reshape(1, -1).astype('float').tolist()[0]
                # 存储
                data_dict['dirName'].append(file_name)
                data_dict['dirPath'].append(file_dir.split("/")[-1])
                data_dict['imgArr'].append(img_arr)
                
# dict to DataFrame
data_df = pd.DataFrame(data_dict)
data_df.query("dirName in ['s1']")
# print(data_df.head(5))

运行结果如下图所示:

                   利用jupyter实现程序时报错,显示UnidentifiedImageErrorTraceback (most recent call last) Cell I_Image_07










【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年12月06日 0

暂无评论

推荐阅读
  P3nxyT0LRuwj   2023年11月19日   17   0   0 数据库ide配置文件
  oIa1edJoFmXP   2023年11月22日   16   0   0 ide数据List
  YKMEHzdP8aoh   2023年12月11日   46   0   0 DNSidePod
  YKMEHzdP8aoh   2023年11月24日   18   0   0 ide重定向Rust
kIM7GUNpnV3x