python 生成png

x33g5p2x  于2022-04-28 转载在 Python  
字(1.1k)|赞(0)|评价(0)|浏览(503)

python opencv生成背景透明图标_Tqdada的博客-CSDN博客

  1. import numpy as np
  2. import cv2
  3. import math
  4. img = np.zeros((230,230), dtype=np.uint8)
  5. img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
  6. img[:,:,:] = 255
  7. #画星号,可以根据需要绘制其他形状
  8. #line1 0°
  9. color = (0,0,0)
  10. width = 55
  11. cv2.line(img, (115, 30), (115, 115), color, width)
  12. #line2 72°
  13. x2 = 115+85*math.sin(0.4*math.pi)
  14. y2 = 115-85*math.cos(0.4*math.pi)
  15. cv2.line(img, (115,115),(int(x2),int(y2)),color,width)
  16. #line3 -72°
  17. x3 = 230-int(x2)
  18. y3 = int(y2)
  19. cv2.line(img, (115,115), (x3,y3), color, width)
  20. #line4 144°
  21. x4 = 115+100*math.sin(0.2*math.pi)
  22. y4 = 115+100*math.cos(0.2*math.pi)
  23. cv2.line(img, (115,115), (int(x4),int(y4)), color, width)
  24. #line5 216°
  25. x5 = 230-int(x4)
  26. y5 = int(y4)
  27. cv2.line(img, (115,115), (x5,y5), color, width)
  28. #创建四通道图片
  29. b,g,r = cv2.split(img)
  30. a = np.ones(b.shape,dtype=b.dtype)*255
  31. for i in range(230):
  32. for j in range(230):
  33. if(b[i][j] == 255 and g[i][j] == 255 and r[i][j] == 255):
  34. a[i][j] = 0
  35. img_al = cv2.merge((b,g,r,a))
  36. #查看保存图片
  37. cv2.imshow("img", img_al)
  38. cv2.imwrite("img.png", img_al)
  39. cv2.waitKey(0)

————————————————
版权声明:本文为CSDN博主「Tqdada」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Tqdada/article/details/105725007

相关文章