I have 2 images I import in R:
image_A <- load.image('C:/Image test/testA.jpg')
image_B <- load.image('C:/Image test/testB.jpg')
I would like to superpose/overlay the image B to A and applying a transparency to image B.
How can I do it ?
I have 2 images I import in R:
image_A <- load.image('C:/Image test/testA.jpg')
image_B <- load.image('C:/Image test/testB.jpg')
I would like to superpose/overlay the image B to A and applying a transparency to image B.
How can I do it ?
Here's one option, using rasterImage from base R.
First lets get two images. For the first we read in the R logo jpeg. Then add another array layer to hold the alpha channel (jpegs do not have transparency)
img.logo = jpeg::readJPEG(system.file("img", "Rlogo.jpg", package="jpeg"))
img.logo = abind::abind(img.logo, img.logo[,,1]) # add an alpha channel
For the second image, lets make it an array the same dimensions as img.1, but fill it with random colors
img.random = img.logo
img.random[] = runif(prod(dim(img.random))) # this image is random colors
Now lets set the base image to fully opaque, and the R logo to semi-transparent
img.logo[,,4] = 0.5 # set alpha to semi-transparent
img.random[,,4] = 1 # set alpha to 1 (opaque)
Now we have our example images, we can superimpose them using 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()