imagecolor package

Module contents

imagecolor initialization.

imagecolor.core_average(image, downsample=True, max_size=100, alpha_threshold=245)[source]

Average a single image.

Averages a single image from a file or file-like object. By default downsamples images that are larger than 100px on the long side for speed. Ignores pixels that are more transperent than the alpha_threshold.

Parameters:
  • image (str) – A filename, pathlib.Path object or a file object.
  • downsample (bool, optional) – if downsampling is enabled to speed up iteration.
  • max_size (int, optional) – max length of longest side if downsample == True.
  • alpha_threshold (int, optional) – level at which transparent pixels are excluded.
Returns:

A dictionary with the following keys: red, green, blue.

Return type:

dict

Raises:
  • IOError – If the file cannot be found, or the image cannot be opened and identified.
  • ImageAveragingError – If the image could not be averaged.
imagecolor.file_average(image, name=None, downsample=True, max_size=100, alpha_threshold=245)[source]

Average a single image and keep track of its file name.

Averages a single image from a file or file-like object. name is extracted from the filepath unless set. By default downsamples images that are larger than 100px on the long side for speed. Ignores pixels that are more transperent than the alpha_threshold.

Parameters:
  • image (str) – A filename, pathlib.Path object or a file object.
  • name (str, optional) – auto generated from path unless set.
  • downsample (bool, optional) – if downsampling is enabled to speed up iteration.
  • max_size (int, optional) – max length of longest side if downsample == True.
  • alpha_threshold (int, optional) – level at which transparent pixels are excluded.
Returns:

A dictionary with the following keys: name, red, green, blue.

Return type:

dict

Raises:
  • AttributeError – If name is not passed in and cannot be set from filepath.
  • IOError – If the file cannot be found, or the image cannot be opened and identified.
  • ImageAveragingError – If the image could not be averaged.
imagecolor.directory_average(path, image_formats=('jpeg', 'png'))[source]

Average all images in a directory.

Accepts the path to a directory and averages each individual image. Uses concurrent.futures to process images in paralell. If images fail to average successfully, the exceptions are caught and logged allowing other images to finish. By default only averages jpeg and png images.

Parameters:
  • path (str) – Path to directory.
  • image_formats (touple of str, optional) – touple of image formats used by imghdr to determine what types of images to average. Defaults: (‘jpeg’, ‘png’)
Returns:

For each image averaged, returns a list of dictionaries each with the following keys: name, red, green, blue.

Return type:

list

Raises:

ImageAveragingError – If no images were averaged successfully.

imagecolor.single_directory_average(path, image_formats=('jpeg', 'png'))[source]

Average all images in a directory into a single average.

Accepts the path to a directory and averages each all images together into a single directory average. Uses concurrent.futures to process images in paralell. If images fail to average successfully, the exceptions are caught and logged allowing other images to finish. By default only averages jpeg and png images.

Parameters:
  • path (str) – Path to directory.
  • image_formats (touple of str, optional) – touple of image formats used by imghdr to determine what types of images to average. Defaults: (‘jpeg’, ‘png’)
Returns:

A dictionary with the following keys: name, red, green, blue.

Return type:

dict

Raises:

DirectoryAveragingError – If the directory could not be averaged.

imagecolor.nested_directory_average(path, image_formats=('jpeg', 'png'))[source]

Averages all subdirectories into a directory average for each directory.

Accepts the path to a directory and walks all the enclosed directories calling single_directory_average for each one that contains images. Uses concurrent.futures to process images in paralell. If images fail to average successfully, the exceptions are caught and logged allowing other images to finish. By default only averages jpeg and png images.

Parameters:
  • path (str) – path to directory
  • image_formats (touple of str, optional) – touple of image formats used by imghdr to determine what types of images to average. Defaults: (‘jpeg’, ‘png’)
Returns:

For each directory averaged, returns a list of dictionaries each with the following keys: name, red, green, blue.

Return type:

list

exception imagecolor.ImageColorException[source]

Bases: Exception

Base Exception for all imagecolor exceptions.

exception imagecolor.ImageAveragingError[source]

Bases: imagecolor.exceptions.ImageColorException

Raised when an image was unable to be averaged.

exception imagecolor.DirectoryAveragingError[source]

Bases: imagecolor.exceptions.ImageColorException

Raised when an directory was unable to be averaged.

exception imagecolor.NoResultsError[source]

Bases: imagecolor.exceptions.ImageColorException

Raised when a list of results is empty or invalid.

imagecolor.results_line(results)[source]

Create a line of pixels from a list of results.

Accepts a list of results and creates an image that is 1 pixel tall and the length of the number of results. The image contains a pixel of the color of each result in the list of results.

Parameters:results (list) – a list of imagecolor results
Returns:linear image containing the results
Return type:PIL.Image.object
imagecolor.results_rectangle(results, aspectratio=(3, 2))[source]

Create a rectangle of pixels from a list of results.

Accepts a list of results and creates an image that is rectangular. The aspect ratio can be set by passing a list formated as [16,9] to aspectratio. The default is 3x2. The image contains a pixel of the color of each result in the list of results.

Parameters:
  • results (list) – a list of imagecolor results.
  • aspectratio (tuple of int) – the aspect ratio of the image being created. Format (3, 2)
Returns:

rectangular image containing the results.

Return type:

PIL.Image.object

imagecolor.results_save_csv(results, path)[source]

Create a csv file from a list of results.

Accepts the path to a new csv file and a list containing results.Writes the current results to a csv file which can be re-loaded again by using csv_to_results. The csv created is formatted as follows: ‘File or Folder’, ‘Red’, ‘Green’, ‘Blue’

Parameters:
  • results (list) – a list of imagecolor results.
  • path (str) – the path to the file to be created.
imagecolor.results_load_csv(path)[source]

Create a list of results from a csv file.

Accepts the path to a csv file formatted as follows: ‘File or Folder’, ‘Red’, ‘Green’, ‘Blue’ parses the file line by line skipping the header. Returns a list containing an list for each line in the csv. Does not do any input checks other than converting the r, g, b colums to ints.

Parameters:path (str) – the path to the file to be loaded.
Returns:a list of imagecolor results.
Return type:list

Submodules

imagecolor.average module

imagecolor functions for averaging images.

imagecolor.average.core_average(image, downsample=True, max_size=100, alpha_threshold=245)[source]

Average a single image.

Averages a single image from a file or file-like object. By default downsamples images that are larger than 100px on the long side for speed. Ignores pixels that are more transperent than the alpha_threshold.

Parameters:
  • image (str) – A filename, pathlib.Path object or a file object.
  • downsample (bool, optional) – if downsampling is enabled to speed up iteration.
  • max_size (int, optional) – max length of longest side if downsample == True.
  • alpha_threshold (int, optional) – level at which transparent pixels are excluded.
Returns:

A dictionary with the following keys: red, green, blue.

Return type:

dict

Raises:
  • IOError – If the file cannot be found, or the image cannot be opened and identified.
  • ImageAveragingError – If the image could not be averaged.
imagecolor.average.directory_average(path, image_formats=('jpeg', 'png'))[source]

Average all images in a directory.

Accepts the path to a directory and averages each individual image. Uses concurrent.futures to process images in paralell. If images fail to average successfully, the exceptions are caught and logged allowing other images to finish. By default only averages jpeg and png images.

Parameters:
  • path (str) – Path to directory.
  • image_formats (touple of str, optional) – touple of image formats used by imghdr to determine what types of images to average. Defaults: (‘jpeg’, ‘png’)
Returns:

For each image averaged, returns a list of dictionaries each with the following keys: name, red, green, blue.

Return type:

list

Raises:

ImageAveragingError – If no images were averaged successfully.

imagecolor.average.file_average(image, name=None, downsample=True, max_size=100, alpha_threshold=245)[source]

Average a single image and keep track of its file name.

Averages a single image from a file or file-like object. name is extracted from the filepath unless set. By default downsamples images that are larger than 100px on the long side for speed. Ignores pixels that are more transperent than the alpha_threshold.

Parameters:
  • image (str) – A filename, pathlib.Path object or a file object.
  • name (str, optional) – auto generated from path unless set.
  • downsample (bool, optional) – if downsampling is enabled to speed up iteration.
  • max_size (int, optional) – max length of longest side if downsample == True.
  • alpha_threshold (int, optional) – level at which transparent pixels are excluded.
Returns:

A dictionary with the following keys: name, red, green, blue.

Return type:

dict

Raises:
  • AttributeError – If name is not passed in and cannot be set from filepath.
  • IOError – If the file cannot be found, or the image cannot be opened and identified.
  • ImageAveragingError – If the image could not be averaged.
imagecolor.average.nested_directory_average(path, image_formats=('jpeg', 'png'))[source]

Averages all subdirectories into a directory average for each directory.

Accepts the path to a directory and walks all the enclosed directories calling single_directory_average for each one that contains images. Uses concurrent.futures to process images in paralell. If images fail to average successfully, the exceptions are caught and logged allowing other images to finish. By default only averages jpeg and png images.

Parameters:
  • path (str) – path to directory
  • image_formats (touple of str, optional) – touple of image formats used by imghdr to determine what types of images to average. Defaults: (‘jpeg’, ‘png’)
Returns:

For each directory averaged, returns a list of dictionaries each with the following keys: name, red, green, blue.

Return type:

list

imagecolor.average.single_directory_average(path, image_formats=('jpeg', 'png'))[source]

Average all images in a directory into a single average.

Accepts the path to a directory and averages each all images together into a single directory average. Uses concurrent.futures to process images in paralell. If images fail to average successfully, the exceptions are caught and logged allowing other images to finish. By default only averages jpeg and png images.

Parameters:
  • path (str) – Path to directory.
  • image_formats (touple of str, optional) – touple of image formats used by imghdr to determine what types of images to average. Defaults: (‘jpeg’, ‘png’)
Returns:

A dictionary with the following keys: name, red, green, blue.

Return type:

dict

Raises:

DirectoryAveragingError – If the directory could not be averaged.

imagecolor.exceptions module

imagecolor module containing all publically raised exceptions.

exception imagecolor.exceptions.DirectoryAveragingError[source]

Bases: imagecolor.exceptions.ImageColorException

Raised when an directory was unable to be averaged.

exception imagecolor.exceptions.ImageAveragingError[source]

Bases: imagecolor.exceptions.ImageColorException

Raised when an image was unable to be averaged.

exception imagecolor.exceptions.ImageColorException[source]

Bases: Exception

Base Exception for all imagecolor exceptions.

exception imagecolor.exceptions.NoResultsError[source]

Bases: imagecolor.exceptions.ImageColorException

Raised when a list of results is empty or invalid.

imagecolor.loadsave module

imagecolor functions for loading and saving results.

imagecolor.loadsave.results_line(results)[source]

Create a line of pixels from a list of results.

Accepts a list of results and creates an image that is 1 pixel tall and the length of the number of results. The image contains a pixel of the color of each result in the list of results.

Parameters:results (list) – a list of imagecolor results
Returns:linear image containing the results
Return type:PIL.Image.object
imagecolor.loadsave.results_load_csv(path)[source]

Create a list of results from a csv file.

Accepts the path to a csv file formatted as follows: ‘File or Folder’, ‘Red’, ‘Green’, ‘Blue’ parses the file line by line skipping the header. Returns a list containing an list for each line in the csv. Does not do any input checks other than converting the r, g, b colums to ints.

Parameters:path (str) – the path to the file to be loaded.
Returns:a list of imagecolor results.
Return type:list
imagecolor.loadsave.results_rectangle(results, aspectratio=(3, 2))[source]

Create a rectangle of pixels from a list of results.

Accepts a list of results and creates an image that is rectangular. The aspect ratio can be set by passing a list formated as [16,9] to aspectratio. The default is 3x2. The image contains a pixel of the color of each result in the list of results.

Parameters:
  • results (list) – a list of imagecolor results.
  • aspectratio (tuple of int) – the aspect ratio of the image being created. Format (3, 2)
Returns:

rectangular image containing the results.

Return type:

PIL.Image.object

imagecolor.loadsave.results_save_csv(results, path)[source]

Create a csv file from a list of results.

Accepts the path to a new csv file and a list containing results.Writes the current results to a csv file which can be re-loaded again by using csv_to_results. The csv created is formatted as follows: ‘File or Folder’, ‘Red’, ‘Green’, ‘Blue’

Parameters:
  • results (list) – a list of imagecolor results.
  • path (str) – the path to the file to be created.