upsies.utils.image

Dump frames from video file

Module Attributes

upsies.utils.image.optimization_levels = ('low', 'medium', 'high', 'placebo', 'none', 'default')

Valid level arguments for optimize()

Functions

upsies.utils.image.convert(image_file, *, mime_type, output_file)[source]

Convert image

Parameters:
  • image_file (str) – Path to image

  • mime_type (str) – Target MIME type to convert image_file to (e.g. “image/png”)

  • output_file (str) – Where to write the converted image to

Raises:

ImageConvertError – if the conversion failed

Returns:

Sanitized output_file (via sanitize_path())

upsies.utils.image.get_mime_type(image_file)[source]

Return MIME type of image_file or None if it cannot be determined

upsies.utils.image.optimize(image_file, output_file=None, level=None)[source]

Optimize PNG image size

Path image_file:

Path to PNG File

Path output_file:

Path to optimized image_file or any falsy value to overwrite image_file

Path str,int level:

Optimiziation level ("low", "medium", "high") or "default" to use recommended level or "none" / None to not do any optimization

If the optimization fails and image_file does not end with “.png”, it is assumed that it is not a PNG and the original file is returned unoptimized.

Returns:

path to optimized PNG file

Raises:

ImageOptimizeError – if the optimization fails

upsies.utils.image.resize(image_file, *, width=0, height=0, target_directory=None, target_filename=None, overwrite=False)[source]

Resize image, preserve aspect ratio

Parameters:
  • image_file – Path to source image

  • width – Desired image width in pixels or 0

  • height – Desired image height in pixels or 0

  • target_directory – Where to put the resized image or None to use the parent directory of image_file

  • target_filename – File name of resized image or None to generate a name from image_file, width and height

  • overwrite (bool) – Whether to overwrite the resized image file if it already exists

If width and height are falsy (the default) return image_file if target_directory and target_filename are falsy or copy image_file to the target path.

Note

It is important to use the returned file path because it is passed through sanitize_path() to make sure it can exist.

Raises:

ImageResizeError – if resizing fails

Returns:

Path to resized or copied image

upsies.utils.image.screenshot(*, video_file, timestamp, screenshot_file, tonemap)[source]

Create single screenshot from video file

Parameters:
  • video_file (str) – Path to video file

  • timestamp (Timestamp (or any int or float)) – Time location in the video

  • screenshot_file (str) – Path to screenshot file

Note

It is important to use the returned file path because it is passed through sanitize_path() to make sure it can exist.

Raises:

ScreenshotError – if something goes wrong

Returns:

Path to screenshot file