Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create transparent image in opencv python

I am trying to make a transparent image and draw on it, and after I will addWeighted over the base image.

How can I initialize fully transparent image with width and hight in openCV python?

EDIT: I want to make a effect like in Photoshop, having stack of the layers, all stacked layers are initially transparent and drawing is performed on fully transparent layer. On the end I will merge all layers to get final image

like image 932
Michael Presečan Avatar asked Jun 16 '17 17:06

Michael Presečan


People also ask

How do you make an image transparent in OpenCV Python?

Just use a new image (e.g. white), draw on it and somehow mark where you are drawing (not needed if you don't draw white as you can check for all pixels != white). Then all non-drawed weights are zero, when you combine these two images. (I'm not an opencv user and i made some assumptions about how addWeighted works).

How do I construct transparent overlays With OpenCV?

Today’s tip comes from my bag of experiences: constructing transparent overlays with OpenCV. In order to construct a transparent overlay, you need two images: Your original image. An image containing what you want to “overlay” on top of the first using some level of alpha transparency.

How to create background transparent of the image in Python?

In this article, the task is to create a background transparent of the image in Python First Install pillow library on your Python Application before going ahead. Python Imaging Library is a free and open-source additional library for the Python programming language that adds support for opening, manipulating, and saving many image file formats.

What is alpha blending in OpenCV?

Well, this tutorial will help you do that in OpenCV. We are sharing code in both C++ and Python. What is alpha blending? Alpha blending is the process of overlaying a foreground image with transparency over a background image. The transparency is often the fourth channel of an image ( e.g. in a transparent PNG), but it can also be a separate image.

Why use transparent overlays in Visual Studio?

Instead of displaying runtime critical information in separate window or in the terminal, you can directly overlay the information on your output image. Using transparent overlays alleviates the need to obfuscate the contents of the output image! A second, more obvious example is alpha transparency where you need to “blend” two images together.


2 Answers

For creating a transparent image you need a 4 channel matrix, 3 of which would represent RGB colors and the 4th channel would represent Alpha channel, To create a transparent image, you can ignore the RGB values and directly set the alpha channel to be 0. In Python OpenCV uses numpy to manipulate matrices, so a transparent image can be created as

import numpy as np
import cv2

img_height, img_width = 300, 300
n_channels = 4
transparent_img = np.zeros((img_height, img_width, n_channels), dtype=np.uint8)

# Save the image for visualization
cv2.imwrite("./transparent_img.png", transparent_img)
like image 145
ZdaR Avatar answered Sep 20 '22 20:09

ZdaR


If you want to draw on several "layers" and then stack the drawings together, then how about this:

import cv2
import numpy as np

#create 3 separate BGRA images as our "layers"
layer1 = np.zeros((500, 500, 4))
layer2 = np.zeros((500, 500, 4))
layer3 = np.zeros((500, 500, 4))

#draw a red circle on the first "layer",
#a green rectangle on the second "layer",
#a blue line on the third "layer"
red_color = (0, 0, 255, 255)
green_color = (0, 255, 0, 255)
blue_color = (255, 0, 0, 255)
cv2.circle(layer1, (255, 255), 100, red_color, 5)
cv2.rectangle(layer2, (175, 175), (335, 335), green_color, 5)
cv2.line(layer3, (170, 170), (340, 340), blue_color, 5)

res = layer1[:] #copy the first layer into the resulting image

#copy only the pixels we were drawing on from the 2nd and 3rd layers
#(if you don't do this, the black background will also be copied)
cnd = layer2[:, :, 3] > 0
res[cnd] = layer2[cnd]
cnd = layer3[:, :, 3] > 0
res[cnd] = layer3[cnd]

cv2.imwrite("out.png", res)

enter image description here

like image 43
Headcrab Avatar answered Sep 17 '22 20:09

Headcrab