如何将图像合并为透明层? [英] How to merge images as transparent layers?

查看:90
本文介绍了如何将图像合并为透明层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为树莓派开发视频编辑器,但是在将图像放置在图像上的速度方面存在问题.当前,使用imagemagick最多需要10秒钟才能在树莓派上使用1080x1920 png图像将一幅图像放置在另一幅图像上,这太多了.随着图像数量的增加,时间也随之增加.关于如何加快速度的任何想法?Imagemagick代码:

I am working on video editor for raspberry pi, and I have a problem with speed of placing image over image. Currently, using imagemagick it takes up to 10 seconds just to place one image over another, using 1080x1920 png images, on raspberry pi, and that's way too much. With the number of images time goes up as well. Any ideas on how to speed it up? Imagemagick code:

composite -blend 90 img1.png img2.png new.png

具有不透明性支持的视频编辑器此处

Video editor with yet slow opacity support here

--------编辑--------

--------EDIT--------

稍微快一点的方法:

import numpy as np
from PIL import Image
size_X, size_Y = 1920, 1080#  put images resolution, else output may look wierd
image1 = np.resize(np.asarray(Image.open('img1.png').convert('RGB')), (size_X, size_Y, 3))
image2 = np.resize(np.asarray(Image.open('img2.png').convert('RGB')), (size_X, size_Y, 3))
output = image1*transparency+image2*(1-transparency)
Image.fromarray(np.uint8(output)).save('output.png')

推荐答案

作为另一种选择,我尝试了pyvips (全面披露:我是pyvips维护者,所以我不是很中立):

Just as another option, I tried in pyvips (full disclosure: I'm the pyvips maintainer, so I'm not very neutral):

#!/usr/bin/python3

import sys
import time
import pyvips

start = time.time()

a = pyvips.Image.new_from_file(sys.argv[1], access="sequential")
b = pyvips.Image.new_from_file(sys.argv[2], access="sequential")
out = a * 0.2 + b * 0.8
out.write_to_file(sys.argv[3])

print("pyvips took {} milliseconds".format(1000 * (time.time() - start)))

pyvips是一个管道"图像处理库,因此代码将并行执行加载,处理和保存所有操作.

pyvips is a "pipeline" image processing library, so that code will execute the load, processing and save all in parallel.

在这两个核心的四线程i5笔记本电脑上,我使用了Mark的两个测试图像:

On this two core, four thread i5 laptop using Mark's two test images I see:

$ ./overlay-vips.py blobs.jpg ships.jpg x.jpg
took 39.156198501586914 milliseconds

因此,两次加载,处理和保存一次jpg所需的时间为39ms.

So 39ms for two jpg loads, processing and one jpg save.

您可以通过将源图像和结果复制到内存中来仅对混合部分进行计时,如下所示:

You can time just the blend part by copying the source images and the result to memory, like this:

a = pyvips.Image.new_from_file(sys.argv[1]).copy_memory()
b = pyvips.Image.new_from_file(sys.argv[2]).copy_memory()

start = time.time()
out = (a * 0.2 + b * 0.8).copy_memory()
print("pyvips between memory buffers took {} milliseconds"
        .format(1000 * (time.time() - start)))

我知道了

$ ./overlay-vips.py blobs.jpg ships.jpg x.jpg 
pyvips between memory buffers took 15.432596206665039 milliseconds

在相同的测试中,numpy大约需要60毫秒.

numpy is about 60ms on this same test.

我尝试了Mark的漂亮numba示例的一些变体:

I tried a slight variant of Mark's nice numba example:

#!/usr/bin/python3

import sys
import time
import numpy as np
from PIL import Image

import numba
from numba import jit, prange

@jit(nopython=True, parallel=True)
def method2(image1, image2, transparency):
   h, w, c = image1.shape
   for y in prange(h):
      for x in range(w):
         for z in range(c):
            image1[y][x][z] = image1[y][x][z] * transparency \
                    + (image2[y][x][z] * (1 - transparency))
   return image1

# run once to force a compile
i1 = np.array(Image.open(sys.argv[1]).convert('RGB'))
i2 = np.array(Image.open(sys.argv[2]).convert('RGB'))
res = method2(i1, i2, 0.2)

# run again and time it
i1 = np.array(Image.open(sys.argv[1]).convert('RGB'))
i2 = np.array(Image.open(sys.argv[2]).convert('RGB'))

start = time.time()
res = method2(i1, i2, 0.2)
print("numba took {} milliseconds".format(1000 * (time.time() - start)))

Image.fromarray(np.uint8(res)).save(sys.argv[3])

我看到:

$ ./overlay-numba.py blobs.jpg ships.jpg x.jpg 
numba took 8.110523223876953 milliseconds

因此,在这台笔记本电脑上,numba比pyvips快约2倍.

So on this laptop, numba is about 2x faster than pyvips.

如果同时加载和保存时间,则速度会慢很多:

If you time load and save as well, it's quite a bit slower:

$ ./overlay-numba.py blobs.jpg ships.jpg x.jpg 
numba plus load and save took 272.8157043457031 milliseconds

但这似乎是不公平的,因为几乎所有时间都在PIL加载和保存中.

But that seems unfair, since almost all that time is in PIL load and save.

这篇关于如何将图像合并为透明层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆