:tocdepth: 3 :py:mod:`gws.lib.image` ======================= .. py:module:: gws.lib.image .. autoapi-nested-parse:: Wrapper for PIL objects **Source code:** :source:`gws.lib.image` Package Contents ---------------- .. py:function:: empty_pixel(mime: str = None) .. py:exception:: Error Bases: :py:obj:`gws.Error` GWS error. .. py:function:: error_pixel(mime: str = None) .. py:function:: from_bytes(r: bytes) -> Image Creates an image object from bytes. :param r: Bytes encoding an image. :returns: An image object. .. py:function:: from_data_url(url: str) -> Optional[Image] Creates an image object from a URL. :param url: URL encoding an image. :returns: An image object. .. py:function:: from_path(path: str) -> Image Creates an image object from a path. :param path: Path to an existing image. :returns: An image object. .. py:function:: 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. :param r: Bytes encoding an image in arrays of pixels. :param mode: PIL image mode. :param size: `(width, height)` :returns: An image object. .. py:function:: from_size(size: gws.Size, color=None) -> Image Creates a monochrome image object. :param size: `(width, height)` :param color: `(red, green, blue, alpha)` :returns: An image object. .. py:function:: from_svg(xmlstr: str, size: gws.Size, mime=None) -> Image Not implemented yet. Should create an image object from a URL. :param xmlstr: XML String of the image. :param size: `(width, height)` :param mime: Mime type. :returns: An image object. .. py:class:: Image(img: PIL.Image.Image) Bases: :py:obj:`gws.Image` Class to convert, save and do basic manipulations on images. .. py:attribute:: img :type: PIL.Image.Image .. py:method:: add_box(color=None) Creates a 1 pixel wide box on the image's edge. :param color: Color of the box's lines. :returns: The image with a box around the edges. .. py:method:: add_text(text, x=0, y=0, color=None) Adds text to an image object. :param text: Text to be displayed. :param x: x-coordinate. :param y: y-coordinate. :param color: Color of the text. :returns: The image object with the text displayed. .. py:method:: 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. .. py:method:: compose(other, opacity=1) Places other image on top of the current image. :param other: Image to place on top. :param opacity: other image's opacity. :returns: The image object with the other image on top as an alpha composition. .. py:method:: crop(box) Crops the image with respect to the given box. :param box: `(width, height)` :returns: The cropped image object. .. py:method:: mode() Get the image mode. :returns: PIL image mode. .. py:method:: paste(other, where=None) Pastes an image to a specific location. :param other: Image that will be placed. :param where: `(x-coord, y-coord)` indicating where the upper left corer should be pasted. :returns: The image object with the other image placed inside. .. py:method:: resize(size, **kwargs) Resizes the image and scales it to fit the new size. :param size: `(width, height)` :returns: The resized image object. .. py:method:: rotate(angle, **kwargs) Rotates the image. :param angle: Angle to rotate the image. :returns: The rotated image object. .. py:method:: size() Get the image size. :returns: A tuple ``(width, height)``. .. py:method:: to_array() Converts the image to an array. :returns: The image as an array. For each row each entry contains the pixel information. .. py:method:: to_base64(mime=None, options=None) Return the image content as a base64 encoded string. .. py:method:: 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). :param mime: The mime type. :param options: A dict of options. :returns: The image as bytes. .. py:method:: to_data_url(mime=None, options=None) Return the image content as a base64-based data url. .. py:method:: to_path(path, mime=None, options=None) Saves the image object at a given path. :param path: Image's path location. :param mime: The mime type. :param options: A dict of options. :returns: The path to the image. .. py:function:: pixel(mime, color) .. py:function:: 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. :param data: Data to encode. :param level: Error correction level, one of L M Q H. :param scale: Box size in pixels. :param border: Include a quiet zone of 4 boxes. :param color: Foreground color. :param background: Background color. .. rubric:: References - https://github.com/lincolnloop/python-qrcode/blob/main/README.rst#advanced-usage