SPPAS 4.20

Module sppas.src.imgdata

Class sppasExtendedImage

Description

Manipulate images represented by a numpy.ndarray of BGR colors.

Extend sppasImage with methods returning a sequence of images.

Public functions

ioverlays

Return a list of the image with other overlaid between coords.

Parameters
  • other: (sppasImage) Image to overlay
  • coord1: (sppasCoords) Position and optionally size to overlay - src
  • coord2: (sppasCoords) Position and optionally size to overlay - dest
  • nb_img: (int) Total number of images
  • blur: (bool)
Returns
  • (list of sppasImage)
View Source
def ioverlays(self, other, coord1, coord2, nb_img=0, blur=False):
    """Return a list of the image with other overlaid between coords.

        :param other: (sppasImage) Image to overlay
        :param coord1: (sppasCoords) Position and optionally size to overlay - src
        :param coord2: (sppasCoords) Position and optionally size to overlay - dest
        :param nb_img: (int) Total number of images
        :param blur: (bool)
        :return: (list of sppasImage)

        """
    images = list()
    coord1 = sppasCoords.to_coords(coord1)
    coord2 = sppasCoords.to_coords(coord2)
    images.append(self.ioverlay(other, coord1))
    if nb_img > 2:
        a, b = slope_intercept((coord1.x, coord1.y), (coord2.x, coord2.y))
        step_x = (coord2.x - coord1.x) / float(nb_img + 1)
        step_y = (coord2.y - coord1.y) / float(nb_img + 1)
        step_w = (coord2.w - coord1.w) / float(nb_img)
        step_h = (coord2.h - coord1.h) / float(nb_img)
        prev_c = coord1
        if blur is True:
            blur_other = other.iblur()
            tr_other = blur_other.ialpha(196, direction=-1)
        for i in range(1, nb_img + 1):
            pimg = self.copy()
            x = max(0, coord1.x + int(step_x * i))
            if coord1.x != coord2.x:
                y = int(linear_fct(x, a, b))
            else:
                y = max(0, coord1.y + int(step_y * i))
            w = max(0, coord1.w + int(step_w * i))
            h = max(0, coord1.h + int(step_h * i))
            c = sppasCoords(x, y, w, h)
            if blur is True and prev_c.x != c.x and (prev_c.y != c.y):
                tr_step_x = (c.x - prev_c.x) // 3
                tr_step_y = (c.y - prev_c.y) // 3
                pimg = self.ioverlay(tr_other, (prev_c.x + tr_step_x, prev_c.y + tr_step_y, prev_c.w, prev_c.h))
                pimg = pimg.ioverlay(tr_other, (prev_c.x + 2 * tr_step_x, prev_c.y + 2 * tr_step_y, prev_c.w, prev_c.h))
                pimg = pimg.ioverlay(tr_other, (prev_c.x + 3 * tr_step_x, prev_c.y + 3 * tr_step_y, prev_c.w, prev_c.h))
            pimg = pimg.ioverlay(other, c)
            images.append(pimg)
            prev_c = c
    images.append(self.ioverlay(other, coord2))
    return images
irotates

Return a list of the image rotated ranging the given angles.

Parameters
  • angle1: (float) Angle start
  • angle2: (float) Angle end
  • center: (tuple) (x,y) position of the rotating center
  • scale: (float) Scale value
  • nb_img: (int) Total number of images
Returns
  • (list of sppasImage)
View Source
def irotates(self, angle1, angle2, center=None, scale=1.0, nb_img=0):
    """Return a list of the image rotated ranging the given angles.

        :param angle1: (float) Angle start
        :param angle2: (float) Angle end
        :param center: (tuple) (x,y) position of the rotating center
        :param scale: (float) Scale value
        :param nb_img: (int) Total number of images
        :return: (list of sppasImage)

        """
    images = list()
    images.append(self.irotate(angle1, center, scale=1.0))
    if nb_img > 2:
        if 0.99 < scale < 1.01:
            step_scale = 0.0
        else:
            step_scale = (scale - 1.0) / float(nb_img + 1)
        step_angle = (angle2 - angle1) / float(nb_img + 1)
        for i in range(1, nb_img + 1):
            s = 1.0 + i * step_scale
            a = angle1 + i * step_angle
            pimg = self.irotate(a, center, s)
            images.append(pimg)
    images.append(self.irotate(angle2, center, scale))
    return images
iscales

Return a list of the image scaled ranging from 1 to the value.

Parameters
  • scale: (float) Scale value
  • nb_img: (int) Total number of images
Returns
  • (list of sppasImage)
View Source
def iscales(self, scale=1.0, nb_img=0):
    """Return a list of the image scaled ranging from 1 to the value.

        :param scale: (float) Scale value
        :param nb_img: (int) Total number of images
        :return: (list of sppasImage)

        """
    return self.irotates(0, 0, None, scale, nb_img)
ifade_in

Fade in the image in nb times from the given color.

Parameters
  • nb_img: (int) Total number of images of the sequence
  • color: BGR of the color
Returns
  • (list of sppasImage)
View Source
def ifade_in(self, nb_img=0, color=(255, 255, 255)):
    """Fade in the image in nb times from the given color.

        :param nb_img: (int) Total number of images of the sequence
        :param color: BGR of the color
        :return: (list of sppasImage)

        """
    images = list()
    img = self.__back.ialpha(value=0, direction=-1)
    w, h = img.size()
    colored_img = sppasImage(0).blank_image(w, h, white=False, alpha=0)
    colored_img = colored_img.iblue(color[0])
    colored_img = colored_img.igreen(color[1])
    colored_img = colored_img.ired(color[2])
    for i in range(nb_img):
        alpha_color = colored_img.ialpha(i * (255 // nb_img))
        trimg = img.ioverlay(alpha_color, (0, 0, w, h))
        images.append(trimg)
    return images