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
Return the raw bytes of the thumbnail.
Return the base64-encoded data of the thumbnail.
Format the thumbnail into a string in the format expected by SciCat.
Attributes
Complete MIME type in the form
type/subtype
.The MIME subtype, i.e., the second part of
type/subtype
.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 formimage/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 withdata
.
- 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 existingThumbnail
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.