欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python生成png的方法

 更新時(shí)間:2024年09月25日 11:57:54   作者:AI算法網(wǎng)奇  
本文主要介紹了python生成png的方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

python 讀取保存png格式透明通道

mask_img=cv2.imread(mask_file, cv2.IMREAD_UNCHANGED)
img=cv2.flip(img,1)
mask_img=cv2.flip(mask_img,1)
cv2.imwrite("_l_mask.png", mask_img)

python讀取保存png圖片

from PIL import Image
# 讀取PNG圖像
image = Image.open("input.png")
# 確保圖片處于正確的模式(RGBA)
image = image.convert("RGBA")
# 保存PNG圖像,同時(shí)保留透明通道
image.save("output.png", "PNG")

python 生成背景透明png

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
#畫星號(hào),可以根據(jù)需要繪制其他形狀
#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)
#創(chuàng)建四通道圖片
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)

到此這篇關(guān)于python生成png的方法的文章就介紹到這了,更多相關(guān)python生成png內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

最新評(píng)論