python生成png的方法
更新時間:2024年09月25日 11:57:54 作者:AI算法網(wǎng)奇
本文主要介紹了python生成png的方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
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圖像,同時保留透明通道
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
#畫星號,可以根據(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python初識二叉樹續(xù)之實戰(zhàn)binarytree
binarytree庫是一個Python的第三方庫,這個庫實現(xiàn)了一些二叉樹相關(guān)的常用方法,使用二叉樹時,可以直接調(diào)用,不需要再自己實現(xiàn),下面這篇文章主要給大家介紹了關(guān)于Python初識二叉樹之實戰(zhàn)binarytree的相關(guān)資料,需要的朋友可以參考下2022-05-05
使用python計算方差方式——pandas.series.std()
這篇文章主要介紹了使用python計算方差方式——pandas.series.std(),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
2022-05-05
python實現(xiàn)的web監(jiān)控系統(tǒng)
這篇文章主要介紹了python實現(xiàn)的web監(jiān)控系統(tǒng),幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
2021-04-04 
