如何处理“OSError:cannotseek back after get firstbytes”在尝试将jpg文件提取到numpy数组时

2024-05-13 01:46:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将zip文件中的所有图像收集到numpy数组中。你知道吗

总是出错: OSError:获取firstbytes后无法返回!你知道吗

import urllib.request
import os
import zipfile
import scipy
import numpy as np
import pandas as pd
import glob
import imageio
from os.path import splitext

url = 'https://github.com/yoavram/Sign-Language/raw/master/Dataset.zip'
filename = '../data/sign-lang'
if not os.path.exists('../data'):
    os.mkdir('../data')
if not os.path.exists(filename):
    urllib.request.urlretrieve(url, filename)

zf = zipfile.ZipFile(filename)

for file in zf.namelist():
    basename,extension = splitext(file)
    if extension == '.jpg':
        with zf.open(file) as img_file:
            img = imageio.read(img_file)

救命啊?你知道吗


Tags: pathimportnumpyimgdataifosrequest
1条回答
网友
1楼 · 发布于 2024-05-13 01:46:34

我有类似问题的时间最长。你知道吗

问题是imageio.read()需要字节,但提供的是文件类型对象。你知道吗

要解决这个问题,只需从文件中读取字节。你知道吗

img = imageio.read(img_file.read())

另外,如果需要numpy数组,应该使用imageio.imread()

相关问题 更多 >