Libvips: 在JPEG上插入具有透明背景的PNG-获取白色背景

创建于 2017-10-25  ·  4评论  ·  资料来源: libvips/libvips

我有一个JPEG:

tile

我要插入PNG:

pin

在JPEG上的128x128位置上。

做的时候

vips insert tile.jpg pin.png newimage.jpg 128 128

它给了我Error Output: insert: images must have the same number of bands, or one must be single-band因为pin.jpg具有Alpha通道,而tile.jpg没有。

所以我将jpg转换为png:

convert tile.jpg png32:tile.png    

现在我有了一个带有Alpha通道的tile.png:
tile

但是当我现在打电话给:

vips insert tile.png pin.png newimage.png 128 128

我得到以下内容:
inserted

插入的pin.png的背景是白色。

我检查了vips insert --help-all但没有真正找到有用的命令行参数。 这两个图像都有一个Alpha通道。 我的问题:

  • libvips insert命令是否支持透明背景?
  • 如果是:有人可以向我指出如何使用此功能吗?
  • ...或者我在这里做错什么了?

乐金
公民

question

最有用的评论

您可以放大叠加层以匹配大图像,然后进行构图:

width=$(vipsheader -f width background.jpg)
height=$(vipsheader -f height background.jpg)
vips embed marker.png overlay.png 128 128 $width $height
vips composite "background.jpg overlay.png" final.jpg 2

或切掉背景的一部分,进行构图,然后再插入(如上)。

这似乎是一件常见的事情,也许应该有一个选项来指定位置。

所有4条评论

你好@codecitizen

insert是一个非常低级的操作:它只是将一个图像放在另一个图像上,它不进行任何混合。

在当前的libvips中,您需要将ifthenelseblend选项一起使用。 这太可怕了:

# get the alpha channel from the overlay
vips extract_band marker.png alpha.png 3 --n 1

# get RGB from the overlay
vips extract_band marker.png rgb.png 0 --n 3

# find the size of the overlay
width=$(vipsheader -f width marker.png)
height=$(vipsheader -f height marker.png)

# cut out an area the size of marker from the background
vips extract_area background.jpg bg.png 128 128 $width $height

# blend the marker on top of the chunk of background
vips ifthenelse alpha.png rgb.png bg.png blended.png --blend

# insert the blended image back into the background
vips insert background.jpg blended.png final.jpg 128 128 

要生成此:

final

libvips 8.6有一个新颖的composite运算符,它可以执行所有常用的混合模式。 你可以做:

vips composite "background.jpg marker.png" final.jpg 2

2表示over混合模式。 它支持所有PDF混合模式。 应该在一周左右的时间内出来。

@jcupitt嘿,谢谢。 我安装了libvips 8.6, vips composite工作得很好。 我只是不确定如何指定第二张图片的位置(如果可能)。

您可以放大叠加层以匹配大图像,然后进行构图:

width=$(vipsheader -f width background.jpg)
height=$(vipsheader -f height background.jpg)
vips embed marker.png overlay.png 128 128 $width $height
vips composite "background.jpg overlay.png" final.jpg 2

或切掉背景的一部分,进行构图,然后再插入(如上)。

这似乎是一件常见的事情,也许应该有一个选项来指定位置。

惊人的! 非常感谢您的快速帮助!

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

AKlein920 picture AKlein920  ·  3评论

kloczek picture kloczek  ·  3评论

Boojs picture Boojs  ·  5评论

volkan picture volkan  ·  5评论

Kyle-Kyle picture Kyle-Kyle  ·  4评论