gws.lib.image.util¶
Image utilities.
thumbnail scales encoded images down, pixel, empty_pixel and error_pixel return
cached encoded 1x1 images. qr_code creates an image with a QR code.
get_draw and get_font give access to PIL drawing on an image.
Source code: gws.lib.image.util
Module Contents¶
- gws.lib.image.util.empty_pixel(ifo: gws.ImageFormat | None = None)¶
Return an encoded empty 1x1 image.
The pixel is transparent, or the background color of the format (white by default) for JPEG.
- Parameters:
ifo – Image format, PNG24 by default.
- Returns:
The image encoded in the first MIME type of the format.
- Raises:
Error – If the MIME type is not an image type.
- gws.lib.image.util.error_pixel(ifo: gws.ImageFormat | None = None)¶
Return an encoded 1x1 image in the error color.
- Parameters:
ifo – Image format, PNG24 by default.
- Returns:
The image encoded in the first MIME type of the format.
- Raises:
Error – If the MIME type is not an image type.
- gws.lib.image.util.get_draw(img: gws.lib.image.core.Image) PIL.ImageDraw.ImageDraw¶
Return a PIL drawing object for an image.
- Parameters:
img – An image object.
- Returns:
A
PIL.ImageDraw.ImageDrawthat draws on the image in place.
- gws.lib.image.util.get_font(size: int = 12, font: str | None = None) PIL.ImageFont.ImageFont | PIL.ImageFont.FreeTypeFont¶
Return a PIL font object.
- Parameters:
size – Font size, used for TrueType fonts only.
font – Path to a TrueType font file, or
Nonefor the PIL default font.
- Returns:
A PIL font object.
- gws.lib.image.util.pixel(mime: gws.MimeType, color)¶
Return an encoded 1x1 image of a color.
Results are cached.
- Parameters:
mime – MIME type, PNG by default.
color – Pixel color, or
Nonefor a transparent pixel.
- Returns:
The encoded image.
- Raises:
Error – If the MIME type is not an image type.
- gws.lib.image.util.qr_code(data: str, level='M', scale=4, border=True, color='black', background='white') gws.lib.image.core.Image¶
Create an image with a QR code.
See https://github.com/lincolnloop/python-qrcode/blob/main/README.rst#advanced-usage.
- Parameters:
data – Data to encode.
level – Error correction level, one of
L,M,Q,H.scale – Box size in pixels.
border – If
True, include a quiet zone of 4 boxes.color – Foreground color.
background – Background color.
- Returns:
An image object.
- Raises:
Error – If the image cannot be created.
- gws.lib.image.util.thumbnail(r: bytes, size: gws.Size, max_pixels=0, mime: gws.MimeType = '', options=None) bytes¶
Create a thumbnail from an encoded image.
The image is scaled to fit into
size, keeping the aspect ratio. Small images are not scaled up.- Parameters:
r – Encoded image.
size – Maximum thumbnail size
(width, height).max_pixels – Maximum number of source pixels,
0for no limit.mime – MIME type of the thumbnail, PNG by default.
options – Encoding options, as in
Image.to_bytes.
- Returns:
The encoded thumbnail.
- Raises:
Error – If the image is too big or cannot be processed.