PIL Image Open无法打开zip文件夹中的某些文件

问题描述

我在一个zip文件夹中大约有300000个图像文件。其中一些文件的路径以“ __”开头。 PIL函数Image.Open()无法打开这些文件。请提出一种打开它们的方法。我的代码如下:

import pandas as pd
import numpy as np
from zipfile import ZipFile
from io import BytesIO
from PIL import Image
from PIL import UnidentifiedImageError

problem_files = []
file_paths = []
img_list = []
img_size = (128,128)
with ZipFile('/XXX/YYY/ZZZ/AI_ML/Project2/words.zip') as myzip:
  contents = myzip.namelist()
  for i in range(0,len(contents)-1):
    text = str(contents[i])
    if '.png' in text:
      file_paths.append(contents[i])
  for path in file_paths:

    img = myzip.read(path)

    try:
      img_data = Image.open(BytesIO(img))
    except UnidentifiedImageError:
      problem_files.append(path)

    img_data = img_data.convert('L')              
    img_data = img_data.resize(img_size)
    image_as_array = np.array(img_data,np.uint8)
    image_as_array = np.reshape(image_as_array,(1,-1))
    img_list.append(image_as_array)

这会将所有路径以'__'开头的文件放入问题文件列表

problem_files[-10:]

['__MACOSX/words/j04/j04-070/._j04-070-08-07.png','__MACOSX/words/j04/j04-070/._j04-070-04-07.png','__MACOSX/words/j04/j04-070/._j04-070-04-06.png','__MACOSX/words/j04/j04-070/._j04-070-08-06.png','__MACOSX/words/j04/j04-070/._j04-070-06-03.png','__MACOSX/words/j04/j04-070/._j04-070-06-01.png','__MACOSX/words/j04/j04-070/._j04-070-08-04.png','__MACOSX/words/j04/j04-070/._j04-070-04-04.png','__MACOSX/words/j04/j04-070/._j04-070-04-05.png','__MACOSX/words/j04/j04-070/._j04-070-08-05.png']

问题文件列表中大约有100000张图像

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)