Image Classes

What are They?

All of the functions in this library are based around the use of Pyifx Image classes. They allow you to store important information about an image in Python while also providing useful functions relating to their properties.

Pyifx Image

The PyifxImage class allows for images to be read, modified, and written in combination with functions provided by the library. This class can either be instantiated through the use of an input and outputh path, or by providing given image data as long as it is in the form of a NumPy ndarray.

Below is an example of what creating instances of the PyifxImage class would look like.

>>> import pyifx
>>> image = pyifx.misc.PyifxImage(input_path="path/to/img.png", output_path="path/to/new_img.png")
>>> image_from_data = pyifx.misc.PyifxImage(input_path=None, output_path="path/to/new_img.png", img=image_data)

Once the class is instantiated, it reads the image located at the specified input path and converts it into a NumPy ndarray. This array can be easily manipulated & worked with to manipulate the represented image.

 >>> image.get_image()
 >>> array([[[174, 173, 213],
 [174, 173, 213],
 [174, 173, 213],
 ...,
 [188, 183, 224],
 [188, 183, 224],
 [188, 183, 224]],

[[174, 173, 213],
 [174, 173, 213],
 [174, 173, 213],
 ...,
 [188, 183, 224],
 [188, 183, 224],
 [188, 183, 224]],

[[174, 173, 213],
 [174, 173, 213],
 [174, 173, 213],
 ...,
 [188, 183, 224],
 [188, 183, 224],
 [188, 183, 224]],

...,

[[ 94, 110, 135],
 [ 94, 110, 135],
 [ 93, 109, 134],
 ...,
 [ 65, 107, 147],
 [ 65, 107, 147],
 [ 65, 107, 147]],

[[ 95, 111, 136],
 [ 94, 110, 135],
 [ 93, 109, 134],
 ...,
 [ 65, 107, 147],
 [ 66, 108, 148],
 [ 66, 108, 148]],

[[ 96, 112, 137],
 [ 95, 111, 136],
 [ 92, 108, 133],
 ...,
 [ 66, 108, 148],
 [ 66, 108, 148],
 [ 67, 109, 149]]], dtype=uint8)

The array is 3-dimensional, with the first dimension representing each row, the second for each pixel, and the third for each channel. The dimensions of the image can be viewed by accessing the shape property of the array.

>>> image.get_image().shape
>>> (1080, 1920, 3)

The reason the height comes before width is due to the fact that the first number represents the number of rows, which makes up the height of the image due to them being stacked on top of each other. The same goes for the width of the image, as well as the image channels.

The methods of this class include:

pyifx.misc.PyifxImage.refresh_image()
pyifx.misc.PyifxImage.get_input_path()
pyifx.misc.PyifxImage.set_input_path()
pyifx.misc.PyifxImage.get_output_path()
pyifx.misc.PyifxImage.set_output_path()
pyifx.misc.PyifxImage.get_image()
pyifx.misc.PyifxImage.set_image()

Note

A full list & description of parameters & methods in the PyifxImage class can be found here or by visiting the library contents page.

Image Volume

The ImageVolume class is a tool used to create and collect PyifxImage instances for a large number of images. Instead of creating these images manually, the class will generate a list of PyifxImage instances based on a provided input directory. The generation method can also be tweaked through adjusting certain parameters when creating instances of the class (ex. Whether to include images from subdirectories.)

On instantiating this class, a ‘volume’ of images will be created based on the specified arguments. Provided below is an example of what using the class might look like.

>>> import pyifx
>>> volume = pyifx.misc.ImageVolume(input_path="lots/of/images/", output_path="lots/of/images/modified/", prefix="_")
>>> print(volume.get_volume())

Running this file will show us what the generated list of images looks like.

>>> [<pyifx.misc.PyifxImage object at 0x0CC66E10>, <pyifx.misc.PyifxImage object at 0x0CC70030>, <pyifx.misc.PyifxImage object at 0x0CC66E50>]

Upon closer inspection, we can see what these images are based off of.

>>> image = volume.get_volume()[0]
>>> image.get_input_path()
>>> "lots/of/images/image_1.jpg"

And if we view the output path of the image, we can see where it leads to.

>>> image.get_output_path()
>>> "lots/of/images/modified/_image_1.jpg"

This is done for every image in the specified directory, and any subdirectories with images in it (if toggled).

The methods of this class include:

pyifx.misc.ImageVolume.volume_to_list()
pyifx.misc.ImageVolume.convert_dir_to_images()
pyifx.misc.ImageVolume.get_input_path()
pyifx.misc.ImageVolume.set_input_path()
pyifx.misc.ImageVolume.get_output_path()
pyifx.misc.ImageVolume.set_output_path()
pyifx.misc.ImageVolume.get_prefix()
pyifx.misc.ImageVolume.set_prefix()
pyifx.misc.ImageVolume.get_volume()
pyifx.misc.ImageVolume.set_volume()

Note

As stated before, a full list & description of parameters & methods in the ImageVolume class can be found here or by visiting the library contents page.