FileArchive#
- class FileArchive(filename: str, filter: str | None = None, loop: bool = False, **kwargs: Any)[source]#
Iterate images from a compressed archive
- Parameters:
filename (str) – path to archive file
filter (str, optional) – a Unix shell-style wildcard that specifies which files to include when iterating over the archive
loop (bool, optional) – endlessly loop over the files, defaults to False
kwargs – options applied to image frames, see
convert
The resulting object is an iterator over the files within the archive. The iterator returns the file as a
Imageinstance if it is an image (thenameattribute is the filename within the archive), or abytesobject for non-image files.The following archive formats are supported:
.zip: stdlib, always available.tar: stdlib, always available.tar.gz/.tgz: stdlib, always available.tar.bz2: stdlib, always available.tar.xz: stdlib, always available.7z: requirespip install py7zr.rar: requiresbrew install unar(macOS) orapt install unar;alternatively
pip install rarfilewithunrar/unarin PATH
If the path is not absolute it is first searched for relative to the current directory, and if not found, it is searched for in the
imagesfolder of themvtb-datapackage, installed as a Toolbox dependency.To read just the image files within the archive, use a
filtersuch as"*.png"or"*.pgm". Note thatfilteris a Unix shell style wildcard expression, not a Python regexp.Example:
from machinevisiontoolbox import FileArchive images = FileArchive('bridge-l.zip') len(images) for image in images: # iterate over files pass
alternatively:
image = images[i] # load i'th file from the archive
or using a context manager to ensure the archive is closed:
with FileArchive('bridge-l.zip') as images: for image in images: pass
- References:
P. Corke, Robotics, Vision & Control for Python, Springer, 2023, Section 11.1.2.
- Seealso:
openconvertcv2.imread
Base constructor for image sources.
- Parameters:
kwargs (Any) – source-specific keyword arguments