imgio

File I/O for saving images and video.

Reading Data from Files

Read images and video data from files for manipulation.

pjimg.imgio.read(path: str | Path) ndarray[tuple[int, ...], dtype[float64]][source]

Read an image or video file.

Parameters:

path – The path to the file.

Returns:

The image or video data as a numpy.ndarray.

Return type:

numpy.ndarray

pjimg.imgio.read_image(filepath: str | Path, as_video: bool = True) ndarray[tuple[int, ...], dtype[float64]][source]

Read image data from an image file.

Parameters:
  • filepath – The location of the image file to read.

  • as_video – (Optional.) Whether the data should be read as a still image or a single frame of video. The difference is video has one more dimension than a still image.

Returns:

A numpy.ndarray object.

Return type:

numpy.ndarray

Usage:

>>> filepath = 'tests/data/__test_save_rgb_image.tiff'
>>> read_image(filepath)
array([[[[1.        , 0.49803922, 0.        ],
         [1.        , 0.49803922, 0.        ],
         [1.        , 0.49803922, 0.        ]],

        [[0.49803922, 0.        , 1.        ],
         [0.49803922, 0.        , 1.        ],
         [0.49803922, 0.        , 1.        ]],

        [[0.        , 1.        , 0.49803922],
         [0.        , 1.        , 0.49803922],
         [0.        , 1.        , 0.49803922]]]])

Note: The imgwriter package works with both image and video data. In an attempt to standardize the output between the two types of data, it treats still images as a single frame video. As a result, it will add a Z axis to image data from still images.

pjimg.imgio.read_video(path: str | Path) ndarray[tuple[int, ...], dtype[float64]][source]

Capture image data from a video file.

Parameters:

path – The path to the file to read.

Returns:

A numpy.ndarray containing the data from the file.

Return type:

numpy.ndarray

Writing Data to Files

Write image and video data to files.

pjimg.imgio.write(filepath: str | Path, a: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], *args, **kwargs) None[source]

Save an array of image data to file.

Parameters:
  • filepath – The location and name of the file that will be saved. The file extension will determine the format used by the file.

  • a – The array of image data.

Returns:

None.

Return type:

None.

Supported File Types

pjimg.imgio uses cv2 for all save operations. The formats supported by cv2 varies depending on operating system and software installed. Since I need to check the file type to determine whether an image or a video is being saved, I have to limit the supported file formats to ones I think are supported across platforms.

pjimg.imgio.write_image(filepath: str | Path, a: ndarray[tuple[int, ...], dtype[uint8]], as_series: bool = True) None[source]

Save an array of image data as an image file.

Parameters:
  • filepath – The location and name of the file that will be saved. The file extension will determine the format used by the file. The data needs to be either in an RGB or grayscale color space.

  • a – The array of image data.

  • as_series – (Optional.) Whether the array is intended to be a series of images.

Returns:

None.

Return type:

None.

pjimg.imgio.write_video(filepath: str | Path, a: ndarray[tuple[int, ...], dtype[uint8]], framerate: float = 12.0, codec: str = 'mp4v') None[source]

Save an array of image data as a video file.

Parameters:
  • filepath – The location and name of the file that will be saved. The file extension will determine the container type used for the file.

  • a – The array of image data.

  • framerate – (Optional.) The number of frames the video will play per second.

  • codec – (Optional.) The codec used to encode the image data into video. The exact list of supported codecs depends upon the operating system. Per the opencv documentation, Linux and Windows will tend to use the list supported by ffmpeg and macOS will use the list suported by QTKit.

Returns:

None.

Return type:

None.