scitacean.Thumbnail#

class scitacean.Thumbnail(mime, data=None, _encoded_data=None)[source]#

Encodes an image to be used as a thumbnail in SciCat.

Thumbnails are small images used, e.g., in attachments. In SciCat, they are base64-encoded strings and have a size limit.

This class handles the encoding but does not enforce a size limit or content type.

Currently, data is stored in encoded form. This means that creating a thumbnail object in a SciCat download is cheap. But creating one from local data and throwing it away without uploading it has some small overhead.

Examples

Given some raw bytes from a PNG, create a thumbnail using

from scitacean import Thumbnail
data = ... # the bytes of the PNG
thumbnail = Thumbnail(mime="image/png", data=data)

Or load the data directly from a file:

thumbnail = Thumbnail.load_file("file_path.png")

Access the raw bytes:

data: bytes = thumbnail.decoded_data()

Constructors

__init__(mime[, data, _encoded_data])

Create a new thumbnail object.

load_file(path)

Construct a thumbnail from data loaded from a file.

parse(encoded, /)

Construct a thumbnail from a string as used by SciCat.

Methods

decoded_data()

Return the raw bytes of the thumbnail.

encoded_data()

Return the base64-encoded data of the thumbnail.

serialize()

Format the thumbnail into a string in the format expected by SciCat.

Attributes

mime

Complete MIME type in the form type/subtype.

mime_subtype

The MIME subtype, i.e., the second part of type/subtype.

mime_type

The MIME type, i.e., the first part of type/subtype.

__init__(mime, data=None, _encoded_data=None)[source]#

Create a new thumbnail object.

Parameters:
  • mime (str | None) – The MIME type of the thumbnail. Must be a string of the form image/png. Parameters are not allowed.

  • data (bytes | None, default: None) – The raw bytes of the thumbnail. Will be encoded automatically.

  • _encoded_data (str | None, default: None) – Primarily for internal use. Base64-encoded data of the thumbnail. Mutually exclusive with data.

decoded_data()[source]#

Return the raw bytes of the thumbnail.

Return type:

bytes

encoded_data()[source]#

Return the base64-encoded data of the thumbnail.

Return type:

str

classmethod load_file(path)[source]#

Construct a thumbnail from data loaded from a file.

Parameters:

path (PathLike[str] | str) – The path to the file.

Returns:

Thumbnail – A new thumbnail with MIME type guessed from the file and data loaded from disk.

mime: str | None#

Complete MIME type in the form type/subtype.

property mime_subtype: str | None#

The MIME subtype, i.e., the second part of type/subtype.

property mime_type: str | None#

The MIME type, i.e., the first part of type/subtype.

classmethod parse(encoded, /)[source]#

Construct a thumbnail from a string as used by SciCat.

Parameters:

encoded (str | Thumbnail) – A string containing a MIME content-header and the thumbnail in base64 encoding. Or an existing Thumbnail instance which is copied on return.

Returns:

Thumbnail – A new thumbnail with MIME type and data extracted from the string.

See also

Thumbnail.serialize

The inverse operation.

serialize()[source]#

Format the thumbnail into a string in the format expected by SciCat.

Returns:

str – A string containing the MIME content-header and the thumbnail in base64 encoding.

See also

Thumbnail.parse

The inverse operation.