Tools for converting images by using Pillow library.
'The Image EXIF orientation tag'

limit_to_max_size[source]

limit_to_max_size(img, max_size)

Limit the image size to max size and scale the image, if max size exceeded. img: The image to validate as Pillow Image. max_size: The max allowed image size. :return: The eventually resized image.

fit_to_max_size[source]

fit_to_max_size(img, max_width, max_height)

Limit the image size to maximum width and height and scale the image, if size exceeded. img: The image to validate as Pillow Image. max_width: The max allowed image width. max_height: The max allowed image height. :return: The eventually resized image.

get_image_size[source]

get_image_size(fname)

Calculates image size of a given image file path. fname: the file path return: the Pillow image, image width and height

get_image_orientation[source]

get_image_orientation(fname)

Parses the EXIF orientation information from the image. fname: the file path :return: The Pillow image and the orientation of the image.

read_exif_metadata[source]

read_exif_metadata(fname)

Read the EXIF metadata information from the image. fname: the file path :return: The Pillow image, EXIF metadata as dictionary or None, if no EXIF data exist.

write_exif_metadata[source]

write_exif_metadata(image, exif_data, fname)

Write the EXIF metadata information to the image. image: the Pillow image to write the EXIF metadata to exif_data: the EXIF metadata as dictionary fname: a file path to store the image :return: True if EXIF metadata saved, else False

assign_exif_orientation[source]

assign_exif_orientation(fname)

Parses the EXIF orientation metadata from the image, rotate the image accordingly and remove the image EXIF orientation metadata. fname: the file path :return: The eventually rotated Pillow image, EXIF metadata with orientation information stripped and True if image rotated, else False

convert_to_base64[source]

convert_to_base64(image, image_type='PNG')

Converts the specified image into a base64 version of itself.

image: The image to transform as Pillow Image. image_type: The image type. :return: The base64 encoded version of the image.

write_mask[source]

write_mask(mask, file_path, palette=None)

Writes a mask as image. The image format is set by the file extension in file_path.

mask: The mask to write. file_path: The path including file name and file extension to write to. palette: The color palette to assign, if set.

Helper Methods

configure_logging[source]

configure_logging(logging_level=20)

Configures logging for the system.

:param logging_level: The logging level to use.

Run from command line

To run the pillow image tools from command line, use the following command: python -m mlcore.image.pillow_tools [parameters]

The following parameters are supported:

  • [image_path]: The path to the image files.