今天小编给大家分享一下怎么用Python为老师送上节日的祝福的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

向日葵

向日葵的花语包括:信念、光辉、忠诚等,很适合老师,我们先画一个简单的向日葵。

实现代码如下:

turtle.setup(600,600,80,80)turtle.pencolor("yellow")turtle.pensize(4)turtle.penup()turtle.fd(-150)turtle.pendown()foriinrange(18):turtle.fd(300)turtle.left(100)turtle.fd(150)turtle.right(90)turtle.pensize(8)turtle.pencolor("green")turtle.fd(400)turtle.penup()turtle.pensize(6)turtle.pendown()#绘制叶子turtle.fd(-250)turtle.seth(45)turtle.circle(-130,60)turtle.seth(-135)turtle.circle(-130,60)turtle.seth(135)turtle.circle(130,60)turtle.seth(-45)turtle.circle(130,60)turtle.done()贺卡

我们还可以利用 Python 在照片中添加一些文字来制作贺卡。

主要实现代码:

img=cv2.imread('test.png')mask=np.zeros(img.shape[:2],np.uint8)size=(1,65)bgd=np.zeros(size,np.float64)fgd=np.zeros(size,np.float64)rect=(1,1,img.shape[1],img.shape[0])cv2.grabCut(img,mask,rect,bgd,fgd,10,cv2.GC_INIT_WITH_RECT)mask2=np.where((mask==2)|(mask==0),1,255)img=img.astype(np.int32)img*=mask2[:,:,np.newaxis]img[img>255]=255img=img.astype(np.uint8)img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)img=Image.fromarray(img,'RGB')img.save('test1.jpg')fp=open(r"word.txt","r",encoding="utf-8")text=fp.read()mask_pic=np.array(Image.open(r"test1.jpg"))wordcloud=WordCloud(font_path='hyr3gjm.ttf',mask=mask_pic,max_words=200).generate(text)image=wordcloud.to_image()image.save("wordcloud2.png")cloud_data=np.array(image)alpha=np.copy(cloud_data[:,:,0])alpha[alpha>0]=255new_image=Image.fromarray(np.dstack((cloud_data,alpha)))card=Image.open("test.png")card=card.convert("RGBA")card.paste(new_image,(0,0),mask=new_image)card.save("card.png")照片墙

我们也可以利用 Python 将一些照片制作成文字效果的照片墙。

主要实现代码:

#将字转化为汉字库的点阵数据defchar2bit(textStr):KEYS=[0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01]target=[]globalcountcount=0forxinrange(len(textStr)):text=textStr[x]rect_list=[]*16foriinrange(16):rect_list.append([]*16)gb2312=text.encode('gb2312')hex_str=binascii.b2a_hex(gb2312)result=str(hex_str,encoding='utf-8')area=eval('0x'+result[:2])-0xA0index=eval('0x'+result[2:])-0xA0offset=(94*(area-1)+(index-1))*32font_rect=Nonewithopen("HZK16","rb")asf:f.seek(offset)font_rect=f.read(32)forkinrange(len(font_rect)//2):row_list=rect_list[k]forjinrange(2):foriinrange(8):asc=font_rect[k*2+j]flag=asc&KEYS[i]row_list.append(flag)output=[]forrowinrect_list:foriinrow:ifi:output.append('1')count+=1else:output.append('0')target.append(''.join(output))returntarget#生成图片文字defhead2char(workspace,folder,self,outlist):#将工作路径转移至头像文件夹os.chdir(folder)#获取文件夹内头像列表imgList=os.listdir(folder)#获取头像图片个数numImages=len(imgList)#设置头像裁剪后尺寸eachSize=100#变量n用于循环遍历头像图片n=0#变量count用于为最终生成的单字图片编号count=0#初始化颜色列表,用于背景着色colorlist=['#FFFACD','#F0FFFF','#BFEFFF','#b7facd','#ffe7cc','#fbccff','#d1ffb8','#febec0','#E0EEE0']#index用来改变不同字的背景颜色index=0#每个item对应不同字的点阵信息foriteminoutlist:#将工作路径转到头像所在文件夹os.chdir(folder)#新建一个带有背景色的画布,16*16点阵,每个点处填充2*2张头像图片,故长为16*2*100canvas=Image.new('RGB',(3200,3200),colorlist[index])#新建一块画布#index变换,用于变换背景颜色index=(index+1)%9count+=1#每个16*16点阵中的点,用四张100*100的头像来填充foriinrange(16*16):#点阵信息为1,即代表此处要显示头像来组字ifitem[i]=="1":#循环读取连续的四张头像图片x1=n%len(imgList)x2=(n+1)%len(imgList)x3=(n+2)%len(imgList)x4=(n+3)%len(imgList)#以下四组try,将读取到的四张头像填充到画板上对应的一个点位置#点阵处左上角图片1/4try:#打开图片img=Image.open(imgList[x1])exceptIOError:print("有1张图片读取失败,已使用备用图像替代")img=Image.open(self)finally:#缩小图片img=img.resize((eachSize,eachSize),Image.ANTIALIAS)#拼接图片canvas.paste(img,((i%16)*2*eachSize,(i//16)*2*eachSize))#点阵处右上角图片2/4try:img=Image.open(imgList[x2])exceptIOError:print("有1张图片读取失败,已使用备用图像替代")img=Image.open(self)finally:img=img.resize((eachSize,eachSize),Image.ANTIALIAS)canvas.paste(img,(((i%16)*2+1)*eachSize,(i//16)*2*eachSize))#点阵处左下角图片3/4try:img=Image.open(imgList[x3])exceptIOError:print("有1张图片读取失败,已使用备用图像替代")img=Image.open(self)finally:img=img.resize((eachSize,eachSize),Image.ANTIALIAS)canvas.paste(img,((i%16)*2*eachSize,((i//16)*2+1)*eachSize))#点阵处右下角图片4/4try:img=Image.open(imgList[x4])exceptIOError:print("有1张图片读取失败,已使用备用图像替代")img=Image.open(self)finally:img=img.resize((eachSize,eachSize),Image.ANTIALIAS)canvas.paste(img,(((i%16)*2+1)*eachSize,((i//16)*2+1)*eachSize))#调整n以读取后续图片n=(n+4)%len(imgList)os.chdir(workspace)#创建文件夹用于存储输出结果ifnotos.path.exists('output'):os.mkdir('output')os.chdir('output')#存储将拼接后的图片,quality为图片质量,1-100,100最高canvas.save('result%d.jpg'%count,quality=100)

以上就是“怎么用Python为老师送上节日的祝福”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。