Python的文字特效,炫酷了!
发布时间:2025-02-09 17:52:33

先上两张效果图


基本结构
总结文本特效的特点是,每个单词都是独立运动的,符合同一运动规律,但每个单词之间保持固定的时差。
每个单词的运动可分为三个部分:字体大小、位置和颜色(透明度)。
#将每个单词与它的三项运动结合成一个基本单位
defnewTextMotion(char,posFunc,sizeFunc,colorFunc):
tm={}
tm['char']=char
tm['posFunc']=posFunc
tm['sizeFunc']=sizeFunc
tm['colorFunc']=colorFunc
returntm展示文字动态效果
在任何时间点获得文本显示效果。
#计算文本在指定时间的位置、大小、颜色等。 defshowText(img,textMotion,time): char=textMotion['char'] pos=textMotion['posFunc'](time) size=textMotion['sizeFunc'](time) color=textMotion['colorFunc'](time) font=ImageFont.truetype(fontName,size) draw=ImageDraw.Draw(im=img) textSize=draw.textsize(text=char,font=font) tx=pos[0]-textSize[0]//2 ty=pos[1]-textSize[1]//2 draw.text(xy=(tx,ty),text=char,fill=color,font=font)
对于一组文本,形成列表,获取每个时间点的显示图作为帧
defgetTextFrame(tmList,time):
textImg=Image.new('RGBA',(1280,720))
fortmintmList:
showText(textImg,tm,time)
returntextImg具体的文字运动规律
让我们来看看这两种特效的具体运动规律。乍一看很复杂,但分为三项运动后,每一项都相对简单。以此为模块,读者可以自己制作更多的文本特效。
#文字缩小 defmakeTextShrink(char,toSize,toPos,toColor,offset,dur): defcolorFunc(time): iftime<offset: return(0,0,0,0) iftime>offset+dur: returntoColor returntoColor[:-1]+(50+round((time-offset)/dur*200),) defsizeFunc(time): iftime<offset: returntoSize*8 iftime>offset+dur: returntoSize returntoSize*8-round((time-offset)/dur*toSize*7.5) defposFunc(time): iftime<offset: return(0,0) iftime>offset+dur: returntoPos #return(toPos[0],round((time-offset)/dur*toPos[1])) returntoPos returnnewTextMotion(char,posFunc,sizeFunc,colorFunc) #抛物线着陆(有回弹效果) defmakeTextParaDrop(char,toSize,toPos,toColor,offset,dur): defcolorFunc(time): iftime<offset: return(0,0,0,0) iftime>offset+dur: returntoColor returntoColor[:-1]+(50+round((time-offset)/dur*200),) defsizeFunc(time): iftime<offset: returntoSize iftime>offset+dur: returntoSize returntoSize defposFunc(time): iftime<offset: return(toPos[0],0) iftime>offset+dur: returntoPos r=0.75 dur2=dur a=toPos[1]/(dur2*dur2*(1-2)*r)) b=-2*a*dur2*r x=(time-offset) return(toPos[0],round(a*x*x+b*x)) #print(toPos) returnnewTextMotion(char,posFunc,sizeFunc,colorFunc)
整体设置和运行
对于一行文字,每一行都会增加特效,并依次给出延迟。
#一行文字,给出所有参数,配置运动函数和延迟 defgetMotionList(text,fontSize,fontColor,startPos,fromTime,dur,func): tmList=[] inter=round(dur/len(text)) foriinrange(len(text)): char=text[i] pos=(startPos[0]+i*fontSize+10,startPos[1]) color=fontColor #tm=makeTextDropMotion(char,fontSize,pos,color,150*i) tm=func(char,fontSize,pos,color,fromTime+inter*i,dur) tmList.append(tm) returntmList
在这里,不同的文本特效函数可以作为参数传输,具有良好的可扩展性。
最后,一个显示函数使用imageio制作gif图。这里有两个地方需要注意。首先,显示时间应该是单文运动时间的两倍。为了确保动态性,当第一个单词到位时,最后一个单词刚刚启动,因此时间是关系的两倍。
二是制作GIF的延迟应与计算中使用的延迟一致,这里是50毫秒(20fps)。
defshowTextDrop(text,startPos,func):
fontSize=50
color=(255,255,0,255)
tmList=getMotionList(text,fontSize,color,startPos,0,1000,func)
frames=[]
outfilename='temp.gif'
foriinrange(0,2000,50):
print(i)
img=Image.new('RGB',(640,360))
#img=Image.open('back.png').resize((640,360),Image.ANTIALIAS)
#img=img.convert("RGB")
textImg=getTextFrame(tmList,i)
r,g,b,a=textImg.split()
img.paste(textImg,(0,0),mask=a)
str1='tempAA.png'
img.save(str1)
im=imageio.imread(str1)
frames.append(im)
imageio.mimsave(outfilename,frames,'GIF',duration=0.05)
if__name__='__main__':
#showTextDrop('淡妆浓妆总合适,#39;,(150,200),makeTextParaDrop)
showTextDrop('淡妆浓妆总是合适的',(150,200),makeTextDropMotion)更多Python知识,请关注Python视频教程!!
