溫馨提示×

溫馨提示×

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

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

Python中怎么生成截圖選餐GIF動畫

發(fā)布時間:2021-07-28 11:30:00 來源:億速云 閱讀:155 作者:Leah 欄目:開發(fā)技術(shù)

Python中怎么生成截圖選餐GIF動畫,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

使用小工具Imagine,并使用動畫作坊打開:

Python中怎么生成截圖選餐GIF動畫

可以看到這張動圖由22張文字圖片組成,幀切換時間為20毫秒。

生成單張圖片

分析完成我們考慮用PIL庫來生成單張圖片,如果還沒有安裝該庫的童鞋,使用以下命令安裝該庫:

pip install pillow

下面選擇了用藍底做背景。我們先來繪制中間的菜名文字:

from PIL import Image, ImageFont, ImageDraw


text = "珍珠土豆燜牛腩"
size = 320
fontsize = (size-20)//len(text)
im = Image.new(mode='RGB', size=(size, size), color="lightblue")

draw = ImageDraw.Draw(im=im)
draw.text(xy=(10, (size-fontsize*1.5)/2),
          text=text, fill=0,
          font=ImageFont.truetype('msyh.ttc', size=fontsize))
im

Python中怎么生成截圖選餐GIF動畫

由于菜品的名字文字個數(shù)不一致,為了都能填滿整圖,作了自動文字大小調(diào)整處理。

字體我選擇了微軟雅黑,當然微軟雅黑也有三種子字體,可以通過系統(tǒng)字體安裝目錄查看字體文件的屬性從而知道字體對應(yīng)的文件名:

Python中怎么生成截圖選餐GIF動畫

下方帶陰影的的文字生成起來會麻煩一些,我的思路是先繪制純黑的文字,在繪制帶黑色邊緣白色填充的文字向上偏移幾個單位:

def text_border(text, x, y, font, shadowcolor, fillcolor):
    draw.text((x - 1, y), text, font=font, fill=shadowcolor)
    draw.text((x + 1, y), text, font=font, fill=shadowcolor)
    draw.text((x, y - 1), text, font=font, fill=shadowcolor)
    draw.text((x, y + 1), text, font=font, fill=shadowcolor)

    draw.text((x - 1, y - 1), text, font=font, fill=shadowcolor)
    draw.text((x + 1, y - 1), text, font=font, fill=shadowcolor)
    draw.text((x - 1, y + 1), text, font=font, fill=shadowcolor)
    draw.text((x + 1, y + 1), text, font=font, fill=shadowcolor)

    draw.text((x, y), text, font=font, fill=fillcolor)


bottomtext = "不知道吃什么?截圖吃飯"
bottom_fontsize = 27
bottom_font = ImageFont.truetype('STHUPO.TTF', size=bottom_fontsize)
x, y = (size-bottom_fontsize*len(bottomtext))/2, size-bottom_fontsize*1.2
draw.text(xy=(x, y), text=bottomtext,
          fill=0, font=bottom_font)
text_border(bottomtext, x, y-4,
            bottom_font, 0, (255, 255, 255))
im

Python中怎么生成截圖選餐GIF動畫

上述代碼選擇了華文琥珀作為字體,個人用來繪制文字邊框的方法比較簡單粗暴,如果有更好的辦法,歡迎留言交流。

考慮到后續(xù)圖片發(fā)送到微信上顯示都很小,干脆現(xiàn)在就壓縮一下像素大?。?/p>

im.thumbnail((128, 128))
im

Python中怎么生成截圖選餐GIF動畫

下面我們封裝一下生成代碼,方便后續(xù)調(diào)用:

from PIL import Image, ImageFont, ImageDraw


def text_img(text, bgcolor="lightblue", bottomtext="不知道吃什么?截圖吃飯", size=360, result_size=(128, 128)):
    def text_border(text, x, y, font, shadowcolor, fillcolor):
        draw.text((x - 1, y), text, font=font, fill=shadowcolor)
        draw.text((x + 1, y), text, font=font, fill=shadowcolor)
        draw.text((x, y - 1), text, font=font, fill=shadowcolor)
        draw.text((x, y + 1), text, font=font, fill=shadowcolor)

        draw.text((x - 1, y - 1), text, font=font, fill=shadowcolor)
        draw.text((x + 1, y - 1), text, font=font, fill=shadowcolor)
        draw.text((x - 1, y + 1), text, font=font, fill=shadowcolor)
        draw.text((x + 1, y + 1), text, font=font, fill=shadowcolor)

        draw.text((x, y), text, font=font, fill=fillcolor)

    im = Image.new(mode='RGB', size=(size, size), color=bgcolor)
    draw = ImageDraw.Draw(im=im)
    fontsize = (size-20)//len(text)
    draw.text(xy=(10, (size-fontsize*1.5)/2),
              text=text, fill=0,
              font=ImageFont.truetype('msyh.ttc', size=fontsize))
    bottom_fontsize = (size-20)//len(bottomtext)
    bottom_font = ImageFont.truetype('STHUPO.TTF', size=bottom_fontsize)
    x, y = (size-bottom_fontsize*len(bottomtext))/2, size-bottom_fontsize*1.2
    draw.text(xy=(x, y), text=bottomtext,
              fill=0, font=bottom_font)
    text_border(bottomtext, x, y-4,
                bottom_font, 0, (255, 255, 255))
    im.thumbnail(result_size)
    return im

測試一下:

text_img("魚香茄子")

Python中怎么生成截圖選餐GIF動畫

ok,現(xiàn)在我們就已經(jīng)能夠給任何菜品生成圖片了。但是菜品的名字哪里來呢?我找到了一個網(wǎng)站,下面考慮爬一下它:

爬取菜品數(shù)據(jù)

網(wǎng)址是:https://m.meishij.net/caipu/

這個網(wǎng)站結(jié)果非常簡單,一個簡單的xpath即可獲取到所有的菜品名稱:

Python中怎么生成截圖選餐GIF動畫

下面開始下載:

from lxml import etree
import requests

req = requests.get("https://m.meishij.net/caipu/")

html = etree.HTML(req.text)
menu = html.xpath("//dl[@class='recipe_list']//a/text()")
menu = list(set([_.strip(".") for _ in menu]))
print(len(menu), menu[:10], menu[-10:])

3744 ['排骨藕湯', '芋圓', '海鮮湯', '涼拌杏鮑菇', '三汁燜鍋', '奶香玉米汁', '炒豆角', '茄子醬', '芒果糯米糍', '饅頭'] ['清蒸茄子', '西蘭花炒雞', '老式蛋糕', '排骨年糕', '清炒絲瓜', '芋頭蒸排骨', '木耳炒肉', '蠔油油麥菜', '麻辣雞塊', '荷葉餅']

有了這些菜名,我們已經(jīng)可以用來生成動圖了。不過為了以后還能夠?qū)W做菜,我們可以將菜名保存起來,要學做菜的時候呢打開網(wǎng)頁:https://so.meishi.cc/?q=菜名,進行搜索。

保存菜名:

with open("meau.csv", "w", encoding="u8") as f:
    f.write("菜名\n")
    for row in menu:
        f.write(row)
        f.write("\n")

下面我們開始生成菜名動圖:

生成菜名動圖

3767多個菜名畢竟是太多,我們可以隨意取30個菜名來生成動圖:

import random

gif_list = random.choices(menu, k=30)
print(gif_list)

['蒸水蛋', '肉桂卷', '涼瓜炒蛋', '芝士焗紅薯', '香蕉酥', '酸奶慕斯', '雞蛋腸粉', '紅油肚絲', '玉米雞蛋餅', '酸辣豆腐湯', '蘿卜燉牛腩', '苦瓜排骨湯', '腐竹拌芹菜', '西紅柿炒土', '蒜蓉蒸茄子', '豆沙面包', '蘑菇炒肉', '清炒蓮藕', '黑椒牛肉粒', '南瓜煎餅', '炒黃瓜', '雜糧饅頭', '桃山皮月餅', '蔥爆肉', '小炒牛肉', '豆瓣鯽魚', '蝦仁燴豆腐', '素餡餃子', '涼拌黃瓜', '砂鍋魚頭']

PS:還是自己選好菜名,寫死列表更好?

import imageio

frames = [text_img(text) for text in gif_list]
imageio.mimsave("meau.gif", frames, 'GIF', duration=0.02)

生成結(jié)果:

Python中怎么生成截圖選餐GIF動畫

根據(jù)菜名列表生成動圖的完整代碼

import imageio
from PIL import Image, ImageFont, ImageDraw


def text_img(text, bgcolor="lightblue", bottomtext="不知道吃什么?截圖吃飯", size=360, result_size=(128, 128)):
    def text_border(text, x, y, font, shadowcolor, fillcolor):
        draw.text((x - 1, y), text, font=font, fill=shadowcolor)
        draw.text((x + 1, y), text, font=font, fill=shadowcolor)
        draw.text((x, y - 1), text, font=font, fill=shadowcolor)
        draw.text((x, y + 1), text, font=font, fill=shadowcolor)

        draw.text((x - 1, y - 1), text, font=font, fill=shadowcolor)
        draw.text((x + 1, y - 1), text, font=font, fill=shadowcolor)
        draw.text((x - 1, y + 1), text, font=font, fill=shadowcolor)
        draw.text((x + 1, y + 1), text, font=font, fill=shadowcolor)

        draw.text((x, y), text, font=font, fill=fillcolor)

    im = Image.new(mode='RGB', size=(size, size), color=bgcolor)
    draw = ImageDraw.Draw(im=im)
    fontsize = (size-20)//len(text)
    draw.text(xy=(10, (size-fontsize*1.5)/2),
              text=text, fill=0,
              font=ImageFont.truetype('msyh.ttc', size=fontsize))
    bottom_fontsize = (size-20)//len(bottomtext)
    bottom_font = ImageFont.truetype('STHUPO.TTF', size=bottom_fontsize)
    x, y = (size-bottom_fontsize*len(bottomtext))/2, size-bottom_fontsize*1.2
    draw.text(xy=(x, y), text=bottomtext,
              fill=0, font=bottom_font)
    text_border(bottomtext, x, y-4,
                bottom_font, 0, (255, 255, 255))
    im.thumbnail(result_size)
    return im


def save_meau_gif(savename, meau):
    frames = [text_img(text) for text in meau]
    imageio.mimsave(savename, frames, 'GIF', duration=0.02)

使用示例:

meau = [
    "荷葉糯米雞", "烤羊肉", "黑椒牛排", "家常大盤雞", "蒜泥豆角",
    "洋蔥炒牛肉", "絲瓜炒雞蛋", "平菇炒雞蛋", "雞刨豆腐", "芙蓉鮮蔬湯",
    "炒西葫蘆", "茄子豆角", "滑蛋牛肉", "香菇青菜", "地三鮮",
    "醬燒杏鮑菇", "腐乳雞翅", "醋溜藕片", "椰子燉雞", "香菇燒豆腐",
    "咖喱雞腿飯", "雞汁土豆泥", "茄子燉土豆", "炒烏冬面", "咖喱土豆雞",
    "上湯娃娃菜", "蒜蓉蒸茄子", "芝士焗紅薯", "栗子黃燜雞", "絲瓜豆腐湯",
]
save_meau_gif("meau.gif", meau)

生成結(jié)果:

Python中怎么生成截圖選餐GIF動畫

自從我們的動圖就生成完畢啦!不知道吃啥的時候都可以拿出來截圖玩玩~?

?祝大家選餐愉快~

PIL操作gif的其他操作

其實用專門動圖處理軟件就可以操作,下面還是補充一下,python的操作API記錄一下:

Gif拆分

比如我們拆分一下這張圖:

Python中怎么生成截圖選餐GIF動畫

from PIL import Image, ImageSequence

img = Image.open('功夫熊.gif')
for i, f in enumerate(ImageSequence.Iterator(img), 1):
    f.save(f'拆分/功夫熊-{i}.png')

拆分結(jié)果:

Python中怎么生成截圖選餐GIF動畫

GIF倒放

下面我們再將上面這張動圖倒放一下:

from PIL import Image, ImageSequence
import imageio

im = Image.open('功夫熊.gif')
sequence = [f.copy() for f in ImageSequence.Iterator(im)]
sequence.reverse()  # 將列表中的幀通過reverse()函數(shù)進行倒序
sequence[0].save('倒放功夫熊.gif', save_all=True, append_images=sequence[1:])

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細節(jié)

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

AI