首页 > 解决方案 > 有快速合成两张图片的功能吗?

问题描述

我正在寻找一种在 Python 或 Cpython 中以最快的方式(0.01s at 1920x1080)合成两个图像的解决方案。

这是一个例子:

在此处输入图像描述

前景图像需要在正确的位置。

这是我的代码:

    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))

标签: pythonopencv

解决方案


推荐阅读