Utilities¶
Various module utilities mostly used from tests.
Factory¶
Imaging¶
- class lotus.utils.imaging.SampleImageCrafter(*args, **kwargs)[source]¶
Craft a basic sample image, either a bitmap or a SVG.
Basically every supported format from PIL should work however this code only knows about JPEG, GIF, PNG and SVG.
- Keyword Arguments:
font (Pil.ImageFont) – A font object to use. This is only used for bitmap image. It is strongly recommended to use a TrueType font. Default is None, this will use the default PIL font. Note than text without a TrueType font will be badly positionned (almost centered but largely shifted).
- get_text_content(text, width, height)[source]¶
Get the text content to include in image.
- Parameters:
text (string or boolean) – Either a string to use it as content or
Trueto make automatic content from given sizes such as320x240(for width value320and height value240). If string is given it should be a short text else it is not guaranteed to fit. If you don’t want text content, just pass a empty string.width (integer) – Width value to display in automatic text content.
height (integer) – Height value to display in automatic text content.
- Returns:
Content to include in image.
- Return type:
string
- get_file_extension(format_name)[source]¶
Get correct file extension depending format name.
- Parameters:
format_name (string) – The format name to use to get the right file extension. It could be any format supported.
JPEGandJPGwill traduces tojpgfile extension.- Returns:
File extension without leading dot.
- Return type:
string
- get_mode(format_name)[source]¶
Get correct image color mode depending format name.
- Parameters:
format_name (string) – The format name to use to get the right file extension. It could be any format supported.
PNGwill be aRGBAmode, every other format will beRGB.- Returns:
Image color mode name.
- Return type:
string
- get_filename(file_extension, filename=None, bg_color=None)[source]¶
Get filename.
- Parameters:
file_extension (string) – File extension to use in automatic filename. Can be anything if
filenameargument is given since it will be ignored.- Keyword Arguments:
filename (string) – Custom filename to use, every other arguments won’t be used to compute filename.
bg_color (string) – A color name to use in automatic filename. For real, this can be anything since it is not validated.
- Returns:
File name.
- Return type:
string
- build(filename=None, size=(100, 100), bg_color='blue', text_color='white', text=None, format_name='PNG')[source]¶
Build config for content creation
- Keyword Arguments:
filename (string) – Custom file name (with file extension) to override the automatic file name (based on other given arguments). Default is
Nonewhich will produce an automatic file name.size (tuple) – A tuple of two integers respectively for width and height. Default to
(100, 100)which will produce a square of 100 pixels.bg_color (string) – Color name to use to paint image background. Default to
blue.text_color (string) – Color name to use to draw possible content text. Default to
white.text (string) – Custom content text to include in image. Default to
Nonewhich will produce an automatic content based on size. Use an empty string to avoid content text in image.format_name (string) – Image format name to use. Default to
PNG.
- Returns:
Configuration to use to create image file.
- Return type:
dict
- create_vectorial(width, height, bg_color, text_content=None, text_color=None)[source]¶
Create SVG content.
- Parameters:
width (integer) – Image width.
height (integer) – Image height.
bg_color (object) – Color name to paint image background.
- Keyword Arguments:
text_content (string) – Content text to include instead of automatic content text.
text_color (string) – Color name to draw text instead of default one. It is required if you want to use the custom context text.
- Returns:
SVG content in a string buffer.
- Return type:
io.StringIO
- create_bitmap(mode, format_name, width, height, bg_color, text_content=None, text_color=None)[source]¶
Create Bitmap image object.
- Parameters:
mode (string) – Image color mode to use.
format_name (string) – Image format name to use.
width (integer) – Image width.
height (integer) – Image height.
bg_color (object) – Color name to paint image background.
- Keyword Arguments:
text_content (string) – Content text to include instead of automatic content text.
text_color (string) – Color name to draw text instead of default one. It is required if you want to use the custom context text.
- Returns:
Image object in a byte buffer.
- Return type:
io.BytesIO
- create(filename=None, size=(100, 100), bg_color='blue', text_color='white', text=None, format_name='PNG')[source]¶
Create an image inside a file object.
Return a File object with a dummy generated image on the fly by PIL or possibly a SVG file.
With default argument values the generated image will be a simple blue square in PNG with no text.
Optionally, you can have any other supported format (JPEG, GIF, SVG), a custom background color and a text.
- Keyword Arguments:
filename (string) – Filename for created file, default to
bg_colorvalue joined to extension withformatvalue in lowercase (orjpgif format isJPEG). Note than final filename may be different if all tests use the same one since Django will append a hash for uniqueness.format_name (string) – Format name as available from PIL:
JPEG,PNGorGIF.SVGformat is also possible to create a dummy SVG file.size (tuple) – A tuple of two integers respectively for width and height. Default to
(100, 100)which will produce a square of 100 pixels.bg_color (string) – Color value to fill image, this should be a valid value for
PIL.ImageColor: https://pillow.readthedocs.io/en/stable/reference/ImageColor.html or a valid HTML color name for SVG format. Default to “blue”. WARNING: If you don’t use named color (like “white” or “yellow”), you should give a custom filename tofilenameargument else the filename may be weird (like#111111.png).text_color (string) – Color value for text. This should be a valid value for
PIL.ImageColor: https://pillow.readthedocs.io/en/stable/reference/ImageColor.html Default to “white”.text (string or boolean) –
Truefor automatic image size like320x240(for width value320and height value240).NoneorFalseto disable text drawing (this is the default value). A string for custom text, this should be a short text else it is not guaranteed to fit.
- Returns:
File object.
- Return type:
object