溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python實(shí)現(xiàn)文字特效的方法

發(fā)布時(shí)間:2020-06-24 10:00:12 來源:億速云 閱讀:909 作者:清晨 欄目:編程語言

不懂Python實(shí)現(xiàn)文字特效的方法?其實(shí)想解決這個(gè)問題也不難,下面讓小編帶著大家一起學(xué)習(xí)怎么去解決,希望大家閱讀完這篇文章后大所收獲。

基本結(jié)構(gòu)

總結(jié)文字特效的特點(diǎn)是,每個(gè)文字獨(dú)立運(yùn)動,都符合同一個(gè)運(yùn)動規(guī)律,但每個(gè)文字之間保持一個(gè)固定的時(shí)間差。

每個(gè)字的運(yùn)動可以分成三個(gè)部分,字體大小的變化、文字位置的變化、文字顏色(透明度)的變化。

# 把每個(gè)文字與它的三個(gè)運(yùn)動結(jié)合為一個(gè)基本單位
def newTextMotion(char, posFunc, sizeFunc, colorFunc):
    tm={}
    tm['char']=char
    tm['posFunc']= posFunc
    tm['sizeFunc']= sizeFunc
    tm['colorFunc']= colorFunc
    return tm

文字動效的展示

在任意一個(gè)時(shí)間點(diǎn)上,獲得文字的顯示效果。

# 在指定的時(shí)間,計(jì)算文字的位置、大小、顏色等
def showText(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)

針對一組文字,形成一個(gè)列表,獲取起每個(gè)時(shí)間點(diǎn)的顯示圖,作為一幀

def getTextFrame(tmList, time):
    textImg= Image.new('RGBA', (1280, 720))
    for tm in tmList:
        showText(textImg, tm, time) 
    return textImg

具體文字運(yùn)動規(guī)律

下面看看這兩種特效的具體運(yùn)動規(guī)律。乍一看比較復(fù)雜,但拆分為三個(gè)運(yùn)動后,其實(shí)每種都比較簡單。以此為模塊,讀者可以自行制作更多的文字特效。

# 文字縮小
def makeTextShrink(char, toSize, toPos, toColor, offset, dur):
    def colorFunc(time):
        if time< offset:
            return (0,0,0,0)
        if time> offset+ dur:
            return toColor
        return toColor[:-1] + (50+ round((time-offset)/dur*200),)
    def sizeFunc(time):
        if time< offset:
            return toSize* 8
        if time> offset+ dur:
            return toSize
        return toSize*8 - round((time-offset)/dur* toSize*7.5)
    def posFunc(time):
        if time< offset:
            return (0,0)
        if time> offset+ dur:
            return toPos
        # return (toPos[0], round((time-offset)/dur*toPos[1]))
        return toPos
    return newTextMotion(char, posFunc, sizeFunc, colorFunc)
# 拋物線降落(有一個(gè)回彈效果)
def makeTextParaDrop(char, toSize, toPos, toColor, offset, dur):
    def colorFunc(time):
        if time< offset:
            return (0,0,0,0)
        if time> offset+ dur:
            return toColor
        return toColor[:-1] + (50+ round((time-offset)/dur*200),)
    def sizeFunc(time):
        if time< offset:
            return toSize
        if time> offset+ dur:
            return toSize
        return toSize
    def posFunc(time):
        if time< offset:
            return (toPos[0], 0)
        if time> offset+ dur:
            return toPos
        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)
    return newTextMotion(char, posFunc, sizeFunc, colorFunc)

整體設(shè)置與運(yùn)行

對于一行文字,每個(gè)增加特效,并依次給予一個(gè)延時(shí)。

# 一行文字,給定所有參數(shù),配置運(yùn)動函數(shù)與延時(shí)
def getMotionList(text, fontSize, fontColor, startPos, fromTime, dur, func):
    tmList=[]
    inter= round(dur/ len(text))
    for i in range(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)
    return tmList

這里,將不同的文字特效函數(shù)作為參數(shù)傳入即可,有比較好的擴(kuò)展性。

最后是一個(gè)展示函數(shù),用了imageio來制作gif圖。這里注意兩個(gè)地方,第一是展示時(shí)間應(yīng)當(dāng)是單文字運(yùn)動時(shí)間的兩倍。為了確保動感,當(dāng)?shù)谝粋€(gè)文字到位時(shí),最后一個(gè)文字恰好啟動,所以時(shí)間是兩倍的關(guān)系。

第二是制作GIF的延時(shí)應(yīng)當(dāng)與計(jì)算用的延時(shí)一致,這里都是50毫秒(20fps)。

def showTextDrop(text, startPos, func):
    fontSize= 50
    color=(255, 255,  0, 255)
    tmList= getMotionList(text, fontSize, color, startPos, 0, 1000, func)
    frames=[]
    outfilename='temp.gif'
    for i in range(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('淡妝濃抹總相宜', (150,200), makeTextParaDrop)
    showTextDrop('淡妝濃抹總相宜', (150,200), makeTextDropMotion)

兩張效果圖

Python實(shí)現(xiàn)文字特效的方法

Python實(shí)現(xiàn)文字特效的方法

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享Python實(shí)現(xiàn)文字特效的方法內(nèi)容對大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,遇到問題就找億速云,詳細(xì)的解決方法等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI