如何使jpg图像半透明

如何使jpg图像半透明

本文介绍了如何使jpg图像半透明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 alpha 通道设置为 .jpg 图像并将其值设置为 0.5 或 50% 或 128(0 到 1 总范围或 0-255 范围),并仅使用 opencv 和 numpy 将其保存为 .png.我知道如何使用其他库来做到这一点,但我们被要求仅使用两个库来执行上述问题,即 import cv2 和 import numpy.我已经添加了 alpha 通道,但我不知道如何将其值设置为 50% 透明度,请帮助我,因为我是 opencv-python 的新手.

我已经尝试过这个代码,但即使我用油漆打开它,我也得到了一个黑色的图像.

使用 OpenCV 添加空白 alpha 通道并使用 Numpy 索引设置内容:

导入 cv2将 numpy 导入为 npimg = cv2.imread('paddington.jpg')# 使用 OpenCV 添加 alpha 层bgra = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)# 使用Numpy索引设置alpha层半透明,B=0, G=1, R=2, A=3bgra[...,3] = 127# 保存结果cv2.imwrite('result.png',bgra)

或者,使用 Numpy dstack() 创建一个填充 128s 的实心 alpha 层并按深度堆叠:

导入 cv2将 numpy 导入为 npimg = cv2.imread('paddington.jpg')# 创建实心alpha层,高度和宽度与img"相同,填充128salpha = np.zeros([img.shape[0],img.shape[1],1], dtype=np.uint8) + 128# 将该层深度堆叠到现有的 3 个 RGB 层上bgra = np.dstack((img,alpha))# 保存结果cv2.imwrite('result.png',bgra)

或者,创建一个填充了 128s 的实心 alpha 层并使用 OpenCV merge() 进行合并:

导入 cv2将 numpy 导入为 npimg = cv2.imread('paddington.jpg')# 创建实心alpha层,高度和宽度与img"相同,填充128salpha = np.full_like(img[...,0], 128)# 使用 OpenCV "merge()" 将新的 alpha 层合并到图像上bgra = cv2.merge((img,alpha))# 保存结果cv2.imwrite('result.png',bgra)

请注意,正如预期的那样,首先描述的 OpenCV cvtColor() 方法最快,大约是 10 倍,因为它是手工优化的 SIMD 代码.给定图像的时间如下:

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

关键词:Python、图像、图像处理、Numpy、OpenCV、dstack、merge、cvtColor、添加alpha通道、添加透明度、设置透明度、COLOR_BGR2BGRA、cv.COLOR_BGR2BGRA

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)
解决方案

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


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)

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

这篇关于如何使jpg图像半透明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 14:40