r - 在R中覆盖2张具有透明度的图像

标签 r image overlay

我在 R 中导入了 2 张图像:

image_A <- load.image('C:/Image test/testA.jpg')
image_B <- load.image('C:/Image test/testB.jpg')

我想将图像 B 叠加/覆盖到 A 并将透明度应用于图像 B。

我该怎么做 ?

最佳答案

这是一个选项,使用 rasterImage从基础 R。

首先让我们得到两张图片。首先,我们在 R Logo jpeg 中阅读。然后添加另一个数组层来保存 alpha channel (jpeg 没有透明度)

img.logo = jpeg::readJPEG(system.file("img", "Rlogo.jpg", package="jpeg"))
img.logo = abind::abind(img.logo, img.logo[,,1]) # add an alpha channel

对于第二张图像,让它成为一个与 img.1 相同尺寸的数组,但用随机颜色填充它
img.random = img.logo
img.random[] = runif(prod(dim(img.random))) # this image is random colors

现在让我们将基础图像设置为完全不透明,并将 R 标志设置为半透明
img.logo[,,4] = 0.5  # set alpha to semi-transparent
img.random[,,4] = 1  # set alpha to 1 (opaque)

现在我们有了示例图像,我们可以使用 rasterImage 叠加它们.
png('test.png', width = 2, height = 2, units = 'in', res = 150)
  par(mai=c(0,0,0,0))
  plot.new()
  rasterImage(img.random, 0, 0, 1, 1)
  rasterImage(img.logo,   0, 0, 1, 1)
dev.off()

enter image description here

关于r - 在R中覆盖2张具有透明度的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56822337/

相关文章:

ffmpeg - 使用 ffmpeg 覆盖图像无限期地运行脚本

iphone - 触摸设备(ipad、iphone)和 100% 宽度和高度的 CSS 顶层?

r - 确定 data.table 组成员的行索引

r - RStudio 和 R 中的运算符 "[<-"

r - 如何从第二页开始在 R Markdown 中进行页码编号?

r - 将月份年份转换为 r 中的日期

image - 在 WP7 中的图像上写文字

html - CSS 图像动画适用于所有浏览器,但 safari

iphone - 将 CGPoints 集转换为 CATransform3D 以校正图像

C# 将控件变灰并在其上绘制另一个控件