python opencv生成背景透明图标

最近项目需要一些特殊形状的图标,然后我试着用opencv生成了一些图标,先看一下效果。
在这里插入图片描述
这个形状主要是用opencv画出来的,以下是代码。

import numpy as np
import cv2
import math

img = np.zeros((230,230), dtype=np.uint8)
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
img[:,:,:] = 255
#画星号,可以根据需要绘制其他形状
#line1 0°
color = (0,0,0)
width = 55
cv2.line(img, (115, 30), (115, 115), color, width)
#line2 72°
x2 = 115+85*math.sin(0.4*math.pi)
y2 = 115-85*math.cos(0.4*math.pi)
cv2.line(img, (115,115),(int(x2),int(y2)),color,width)
#line3 -72°
x3 = 230-int(x2)
y3 = int(y2)
cv2.line(img, (115,115), (x3,y3), color, width)
#line4 144°
x4 = 115+100*math.sin(0.2*math.pi)
y4 = 115+100*math.cos(0.2*math.pi)
cv2.line(img, (115,115), (int(x4),int(y4)), color, width)
#line5 216°
x5 = 230-int(x4)
y5 = int(y4)
cv2.line(img, (115,115), (x5,y5), color, width)
#创建四通道图片
b,g,r = cv2.split(img)
a = np.ones(b.shape,dtype=b.dtype)*255
for i in range(230):
    for j in range(230):
        if(b[i][j] == 255 and g[i][j] == 255 and r[i][j] == 255):
            a[i][j] = 0
img_al = cv2.merge((b,g,r,a))
#查看保存图片
cv2.imshow("img", img_al)
cv2.imwrite("img.png", img_al)
cv2.waitKey(0)

还有一些其他的形状。
在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用OpenCVPython中将图片背景变成白色并使其透明。代码如下: ```python import cv2 # 读取图片 img = cv2.imread("image.png") # 转换为灰度图 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 对图片进行二值化 _, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY) # 找到轮廓 contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 获取最大轮廓 cnt = max(contours, key=cv2.contourArea) # 创建遮罩 mask = np.zeros(img.shape[:2], np.uint8) # 在遮罩上绘制轮廓 cv2.drawContours(mask, [cnt], 0, (255, 255, 255), -1) # 应用遮罩 result = cv2.bitwise_and(img, img, mask=mask) # 将白色背景设置为透明 result[np.where((result == [255,255,255]).all(axis = 2))] = [255,255,255,0] # 保存图片 cv2.imwrite("transparent_image.png", result) ``` 请注意,上面代码假定您要将背景变成白色,并且将其设置为完全透明。如果您需要其他颜色或不透明度,则需要进行修改。 ### 回答2: 要使用Python代码实现OpenCV生成白底透明图,可以按照以下步骤进行: 1. 导入必要的库和模块: ```python import cv2 import numpy as np ``` 2. 创建一个空白的图像: ```python width, height = 500, 500 # 设置图像的宽度和高度 image = np.zeros((height, width, 4), dtype=np.uint8) # 创建一个宽高为500x500像素,4个通道的空白图像 ``` 3. 填充图像的每个像素为白色,并将透明通道设置为完全不透明: ```python image[:, :, 0:3] = 255 # 将图像的RGB通道设置为255,即白色 image[:, :, 3] = 255 # 将图像的透明通道设置为完全不透明 ``` 4. 保存图像: ```python cv2.imwrite("transparent_image.png", image) # 将图像保存为透明图 ``` 这样,我们就成功使用Python代码实现OpenCV生成了一个白底透明图。生成的图像大小为500x500像素,拥有四个通道,RGB通道设置为白色(255, 255, 255),透明通道设置为完全不透明(255)。 ### 回答3: 要实现Python代码生成白底透明图,需要使用OpenCV库和Python的图像处理技巧。 1. 首先,导入必要的库: ```python import cv2 import numpy as np ``` 2. 创建一个白色底图,大小为500x500像素,像素值为255(即白色): ```python width, height = 500, 500 image = np.ones((height, width, 3), np.uint8) * 255 ``` 3. 将底图的通道设置为透明,即Alpha通道全为0: ```python image[:, :, 3] = 0 ``` 4. 显示和保存生成的白底透明图: ```python cv2.imshow('Transparent Image', image) cv2.imwrite('transparent_image.png', image) cv2.waitKey(0) ``` 完整的代码如下: ```python import cv2 import numpy as np # 创建白底透明图 width, height = 500, 500 image = np.ones((height, width, 3), np.uint8) * 255 image[:, :, 3] = 0 # 显示和保存透明图 cv2.imshow('Transparent Image', image) cv2.imwrite('transparent_image.png', image) cv2.waitKey(0) ``` 运行代码后,将会生成一个500x500像素的白色底图,并且底图是透明的。你可以通过显示窗口和保存为PNG格式的文件来查看生成的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值