Is there a function for compositing two images fast?

Viewed 159

I'm searching for an solution to composite two images at the fastest way (0.01s at 1920x1080) in Python or Cpython.

Here is an example:

enter image description here

The foreground Image needs to be at the right position.

Here is my slow code:

    def alpha_composite(self, im, dest=(0, 0)):
        """ 'In-place' analog of Image.alpha_composite. Composites an image
        onto this image.

        :param im: image to composite over this one
        :param dest: Optional 2 tuple (left, top) specifying the upper
          left corner in this (destination) image.

        Performance Note: Not currently implemented in-place in the core layer.
        """

        (w, h) = self.size
        (wH, wW) = im._instance.shape[:2]
        (B, G, R, A) = cv2.split(im._instance)
        B = cv2.bitwise_and(B, B, mask=A)
        G = cv2.bitwise_and(G, G, mask=A)
        R = cv2.bitwise_and(R, R, mask=A)
        im._instance = cv2.merge([B, G, R, A])
        overlay = np.zeros((h, w, 4), dtype=np.float32)
        overlay[dest[1]:wH + dest[1], dest[0]:wW + dest[0]] = im._instance
        cv2.addWeighted(overlay.astype(np.float32), 0.4, self._instance.astype(
            np.float32), 1.0, 0, self._instance.astype(np.float32))
0 Answers
Related