3

I want set alpha channel to a .jpg image and set its value to 0.5 or 50% or at 128 (0 to 1 total range or 0-255 range) and save it as .png using only opencv and numpy. I know how to do it using other libraries but we have been asked to perform the above question using only two library i.e. import cv2 and import numpy. i have added the alpha channel but i do not know how to set its value to 50% transparency, please help as I am new to opencv-python.

I have tried this code but I am getting a black image even when I open it with paint.

Reduce opacity of image using Opencv in Python

this is how I have added my alpha

import cv2
import numpy as np
img = cv2.imread('food.jpg')
bgra = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)
print(bgra.shape)
Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
  • Does this answer your question? [python OpenCV - add alpha channel to RGB image](https://stackoverflow.com/questions/32290096/python-opencv-add-alpha-channel-to-rgb-image) – ZdaR Oct 31 '19 at 07:45
  • Please refer to the above answer and simply replace the `50` with `127` at the step I am creating the alpha channel. – ZdaR Oct 31 '19 at 07:45
  • It should be understood that JPEG format images can not have transparency. The image that is read into from a .jpg file can have transparency, but that is an OpenCV internal image. If that image is then written out to a .jpg file (i.e., in JPEG format), the transparency will be lost.To preserve transparency, the output file needs to be in a format that supports transparency, like PNG. https://stackoverflow.com/questions/16906144/transparent-background-in-jpeg-image/16906178 – Paul Richter Oct 31 '19 at 12:02
  • ZdaR, I have tried the above link but did not replace it with 127 then. – Daigo Hyakkimaru Nov 01 '19 at 04:20

1 Answers1

6

There are several ways of doing this... I'll start with this image:

enter image description here


Add blank alpha channel with OpenCV and set content with Numpy indexing:

import cv2 
import numpy as np 
img = cv2.imread('paddington.jpg')

# Add alpha layer with OpenCV
bgra = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA) 

# Set alpha layer semi-transparent with Numpy indexing, B=0, G=1, R=2, A=3
bgra[...,3] = 127

# Save result
cv2.imwrite('result.png',bgra)

enter image description here


Alternatively, create a solid alpha layer filled with 128s and stack depth-wise with Numpy dstack():

import cv2 
import numpy as np 
img = cv2.imread('paddington.jpg')

# Create solid alpha layer, same height and width as "img", filled with 128s
alpha = np.zeros([img.shape[0],img.shape[1],1], dtype=np.uint8) + 128

# Depth-wise stack that layer onto existing 3 RGB layers
bgra = np.dstack((img,alpha))

# Save result
cv2.imwrite('result.png',bgra)

Alternatively, create a solid alpha layer filled with 128s and merge using OpenCV merge():

import cv2 
import numpy as np 
img = cv2.imread('paddington.jpg')

# Create solid alpha layer, same height and width as "img", filled with 128s
alpha = np.full_like(img[...,0], 128)

# Merge new alpha layer onto image with OpenCV "merge()"
bgra = cv2.merge((img,alpha))

# Save result
cv2.imwrite('result.png',bgra)

Note that, as expected, the OpenCV cvtColor() method described first is fastest, by a factor of about 10x because it is hand optimised SIMD code. Timings with given image were as follows:

  • cv2.cvtColor() - 48 microseconds
  • np.dstack() - 477 microseconds
  • cv2.merge() - 489 microseconds

Keywords: Python, image, image processing, Numpy, OpenCV, dstack, merge, cvtColor, add alpha channel, add transparency, set transparency, COLOR_BGR2BGRA, cv.COLOR_BGR2BGRA

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432