gpt4 book ai didi

python - 使用 ImageIO 从 ZipFile 处理文件

转载 作者:行者123 更新时间:2023-12-01 02:27:10 33 4
gpt4 key购买 nike

我尝试使用 imageio 从 zip 文件读取图像文件时遇到错误:

import zipfile
import glob
import imageio
from os.path import splitext

for database in glob.iglob('Datasets/*.zip'):
print(database)
zf = zipfile.ZipFile(database, 'r')
for file in zf.namelist():
basename,extension = splitext(file)
if extension == '.png':
img = imageio.imread(file)
print(img.shape, end='')

这是回溯:

Datasets/first.zip
Traceback (most recent call last):
File "testZip.py", line 12, in <module>
img = imageio.imread(file)
File "/usr/local/anaconda3/lib/python3.6/site-packages/imageio/core/functions.py", line 200, in imread
reader = read(uri, format, 'i', **kwargs)
File "/usr/local/anaconda3/lib/python3.6/site-packages/imageio/core/functions.py", line 117, in get_reader
request = Request(uri, 'r' + mode, **kwargs)
File "/usr/local/anaconda3/lib/python3.6/site-packages/imageio/core/request.py", line 120, in __init__
self._parse_uri(uri)
File "/usr/local/anaconda3/lib/python3.6/site-packages/imageio/core/request.py", line 252, in _parse_uri
raise IOError("No such file: '%s'" % fn)
OSError: No such file: 'first/image_7395.png'
[Finished in 0.2s with exit code 1]

最佳答案

您正在尝试按名称打开 ZIP 中的图像。 imageio 不知道该怎么做。它假设您给了它一个真实的文件路径。您需要首先打开 zip 中的文件,为其提供一个文件对象。您也可以像@randomdude999建议的那样先提取它。

for file in zf.namelist():
basename,extension = splitext(file)
if extension == '.png':
img = imageio.imread(zf.open(file))
print(img.shape, end='')

关于python - 使用 ImageIO 从 ZipFile 处理文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47269222/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com