gws.lib.image

Wrapper for PIL objects

Source code: gws.lib.image

Package Contents

gws.lib.image.empty_pixel(mime: str = None)
exception gws.lib.image.Error

Bases: gws.Error

GWS error.

gws.lib.image.error_pixel(mime: str = None)
gws.lib.image.from_bytes(r: bytes) Image

Creates an image object from bytes.

Parameters:

r – Bytes encoding an image.

Returns:

An image object.

gws.lib.image.from_data_url(url: str) Image | None

Creates an image object from a URL.

Parameters:

url – URL encoding an image.

Returns:

An image object.

gws.lib.image.from_path(path: str) Image

Creates an image object from a path.

Parameters:

path – Path to an existing image.

Returns:

An image object.

gws.lib.image.from_raw_data(r: bytes, mode: str, size: gws.Size) Image

Creates an image object in a given mode from raw pixel data in arrays.

Parameters:
  • r – Bytes encoding an image in arrays of pixels.

  • mode – PIL image mode.

  • size(width, height)

Returns:

An image object.

gws.lib.image.from_size(size: gws.Size, color=None) Image

Creates a monochrome image object.

Parameters:
  • size(width, height)

  • color(red, green, blue, alpha)

Returns:

An image object.

gws.lib.image.from_svg(xmlstr: str, size: gws.Size, mime=None) Image

Not implemented yet. Should create an image object from a URL.

Parameters:
  • xmlstr – XML String of the image.

  • size(width, height)

  • mime – Mime type.

Returns:

An image object.

class gws.lib.image.Image(img: PIL.Image.Image)

Bases: gws.Image

Class to convert, save and do basic manipulations on images.

add_box(color=None)

Creates a 1 pixel wide box on the image’s edge.

Parameters:

color – Color of the box’s lines.

Returns:

The image with a box around the edges.

add_text(text, x=0, y=0, color=None)

Adds text to an image object.

Parameters:
  • text – Text to be displayed.

  • x – x-coordinate.

  • y – y-coordinate.

  • color – Color of the text.

Returns:

The image object with the text displayed.

compare_to(other)

Compare this image to another one.

@TODO describe the alogrithm

Returns:

The similarity factor as a float (the more, the different). ‘0’ means images are equal.

compose(other, opacity=1)

Places other image on top of the current image.

Parameters:
  • other – Image to place on top.

  • opacity – other image’s opacity.

Returns:

The image object with the other image on top as an alpha composition.

crop(box)

Crops the image with respect to the given box.

Parameters:

box(width, height)

Returns:

The cropped image object.

mode()

Get the image mode.

Returns:

PIL image mode.

paste(other, where=None)

Pastes an image to a specific location.

Parameters:
  • other – Image that will be placed.

  • where(x-coord, y-coord) indicating where the upper left corer should be pasted.

Returns:

The image object with the other image placed inside.

resize(size, **kwargs)

Resizes the image and scales it to fit the new size.

Parameters:

size(width, height)

Returns:

The resized image object.

rotate(angle, **kwargs)

Rotates the image.

Parameters:

angle – Angle to rotate the image.

Returns:

The rotated image object.

size()

Get the image size.

Returns:

A tuple (width, height).

to_array()

Converts the image to an array.

Returns:

The image as an array. For each row each entry contains the pixel information.

to_base64(mime=None, options=None)

Return the image content as a base64 encoded string.

to_bytes(mime=None, options=None)

Converts the image object to bytes.

The options dict can contain any PIL save option (see https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html).

An additional option mode is the image mode (see https://pillow.readthedocs.io/en/stable/handbook/concepts.html#concept-modes). If provided, the image is converted to that mode before saving.

An additional option background sets the color to replace the alpha channel with when converting from RGBA to RGB (default is white).

Parameters:
  • mime – The mime type.

  • options – A dict of options.

Returns:

The image as bytes.

to_data_url(mime=None, options=None)

Return the image content as a base64-based data url.

to_path(path, mime=None, options=None)

Saves the image object at a given path.

Parameters:
  • path – Image’s path location.

  • mime – The mime type.

  • options – A dict of options.

Returns:

The path to the image.

gws.lib.image.pixel(mime, color)
gws.lib.image.qr_code(data: str, level='M', scale=4, border=True, color='black', background='white') Image

Creates an Image with a QR code for the given data.

Parameters:
  • data – Data to encode.

  • level – Error correction level, one of L M Q H.

  • scale – Box size in pixels.

  • border – Include a quiet zone of 4 boxes.

  • color – Foreground color.

  • background – Background color.

References