gws.lib.image

Wrapper for PIL objects

Source code: gws.lib.image

Package Contents

exception gws.lib.image.Error

Bases: gws.Error

GWS error.

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_bytes(r: bytes) Image

Creates an image object from bytes.

Parameters:

r – Bytes encoding an image.

Returns:

An image object.

gws.lib.image.ImageMode

Our accepted image modes.

gws.lib.image.from_raw_data(r: bytes, mode: ImageMode, 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 – The mode encoding the pixels.

  • size(width, height)

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_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_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.

size() gws.Size

The image’s size.

Returns:

Length and width of the image.

resize(size, **kwargs) Image

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

Parameters:

size(width, height)

Returns:

The resized image object.

rotate(angle, **kwargs) Image

Rotates the image.

Parameters:

angle – Angle to rotate the image.

Returns:

The rotated image object.

crop(box) Image

Crops the image with respect to the given box.

Parameters:

box(width, height)

Returns:

The cropped image object.

paste(other, where=None) Image

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.

compose(other, opacity=1) Image

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.

to_bytes(mime=None) bytes

Converts the image object to bytes.

Parameters:

mime – The mime type.

Returns:

The image as bytes.

to_path(path, mime=None) str

Saves the image object at a given path.

Parameters:
  • path – Image’s path location.

  • mime – The mime type.

Returns:

The path to the image.

to_array()

Converts the image to an array.

Returns:

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

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

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.

add_box(color=None) Image

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.

getpixel(xy: tuple[int, int])
gws.lib.image.PIXEL_PNG8 = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x03\x00\x00\x00(\xcb4\x...

1x1 empty image in png8 format

gws.lib.image.PIXEL_PNG24 = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00\x1f\x15...

1x1 empty image in png24 format

gws.lib.image.PIXEL_JPEG_BLACK = b'\xff\xd8\xff\xdb\x00C\x00\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x...

1x1 empty image in jpeg format

gws.lib.image.PIXEL_JPEG_WHITE = b'\xff\xd8\xff\xdb\x00C\x00\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x...

1x1 empty image in jpeg format

gws.lib.image.PIXEL_GIF = b'GIF89a\x01\x00\x01\x00\x80\x00\x00\xff\xff\xff\x00\x00\x00!\xf9\x04\x01\x00\x00\x00\x00,\x00\x0...

1x1 empty image in gif format