画星星函数:
#img 图片
#x y 坐标
#size 大小
def darw_star(img,x,y,color,size=40):
poly_line=np.array([[x, y-size], [x+int(size/4), y-int(size/4)], [x+size, y-int(size/4)],[x+int(0.375*size),y+int(size/4)],[x+size,y+size],\
[x,y+int(0.625*size)],[x-size,y+size],[x-int(0.375*size),y+int(size/4)],[x-size,y-int(size/4)],[x-int(size/4),y-int(size/4)]], np.int32)
cv2.polylines(img,[poly_line],True,color,4,16)
代码如下:
import cv2
import numpy as np
import random
img=np.zeros([640,640,3]).astype(np.uint8)
img=img.ravel()
for i,j in enumerate(img):
if j==0:
img[i]=255
img=np.reshape(img,[640,640,3])
i=50
j=30
for num in range(6):
r = random.randint(0, 255)
b = random.randint(0, 255)
g = random.randint(0, 255)
cv2.ellipse(img,[320,50],[i,j],60,0,60,[b,g,r],4,16)
i+=80
j+=80
i=30
j=10
for num in range(6):
r = random.randint(0, 255)
b = random.randint(0, 255)
g = random.randint(0, 255)
cv2.ellipse(img,[320,50],[i,j],60,0,60,[b,g,r],4,16)
i+=80
j+=80
i=70
j=50
for num in range(5):
r = random.randint(0, 255)
b = random.randint(0, 255)
g = random.randint(0, 255)
cv2.ellipse(img,[320,50],[i,j],60,0,60,[b,g,r],4,16)
i+=80
j+=80
i=90
j=70
for num in range(5):
r = random.randint(0, 255)
b = random.randint(0, 255)
g = random.randint(0, 255)
cv2.ellipse(img,[320,50],[i,j],60,0,60,[b,g,r],4,16)
i+=80
j+=80
def darw_star(x,y,color,size=40):
global img
poly_line=np.array([[x, y-size], [x+int(size/4), y-int(size/4)], [x+size, y-int(size/4)],[x+int(0.375*size),y+int(size/4)],[x+size,y+size],\
[x,y+int(0.625*size)],[x-size,y+size],[x-int(0.375*size),y+int(size/4)],[x-size,y-int(size/4)],[x-int(size/4),y-int(size/4)]], np.int32)
cv2.polylines(img,[poly_line],True,color,4,16)
for i in range(50):
darw_star(325,50,[0,255,255],i)
darw_star(325,50,[0,0,0],50)
for i in range(40):
darw_star(320,200,[0,0,255],i)
darw_star(320,200,[0,0,0],40)
for i in range(30):
darw_star(370,300,[0,255,255],i)
darw_star(370,300,[0,0,0],30)
for i in range(30):
cv2.circle(img,(420,450),i,[0,255,0])
cv2.circle(img,(420,450),30,[0,0,0],4,16)
for i in range(30):
cv2.circle(img,(220,450),i,[0,0,255])
cv2.circle(img,(220,450),30,[0,0,0],4,16)
for i in range(30):
cv2.circle(img,(200,300),i,[0,255,255])
cv2.circle(img,(200,300),30,[0,0,0],4,16)
for i in range(30):
cv2.circle(img,(450,300),i,[255,100,0])
cv2.circle(img,(450,300),30,[0,0,0],4,16)
for i in range(30):
darw_star(340,420,[255,0,255],i)
darw_star(340,420,[0,0,0],30)
for i in range(40):
darw_star(150,380,[255,100,255],i)
darw_star(150,380,[0,0,0],40)
cv2.putText(img,'Merry Christmas',[80,560],cv2.FONT_HERSHEY_SIMPLEX,2,[0,0,255],thickness=4)
cv2.imshow('fr',img)
cv2.waitKey(0)
cv2.destroyAllWindows()