您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么用python寫一個福字”,在日常操作中,相信很多人在怎么用python寫一個福字問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么用python寫一個福字”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
支付寶 2022 集五福活動正式開啟
數據顯示,過去六年累計參與支付寶集五福的人數已經超過了 7 億,每 2 個中國人里就有 1 個曾掃福、集福、送福。
首先作品奉上:
①,導入python庫
import io from PIL import Image import requests
②,利用爬蟲,獲取單個漢字
def get_word(ch, quality): fp = io.BytesIO(requests.post(url='http://xufive.sdysit.com/tk', data={'ch':ch}).content) im = Image.open(fp) w, h = im.size if quality == 'M': w, h = int(w*0.75), int(0.75*h) elif quality == 'L': w, h = int(w*0.5), int(0.5*h) return im.resize((w,h))
def get_word 的作用為爬取我們需要的漢字模型。
如圖:
③,爬取背景底圖
def get_bg(quality): return get_word('bg', quality)
④,圖片格式大小,配置函數
def write_couplets(text, HorV='V', quality='L', out_file=None): usize = {'H':(640,23), 'M':(480,18), 'L':(320,12)} bg_im = get_bg(quality) text_list = [list(item) for item in text.split()] rows = len(text_list) cols = max([len(item) for item in text_list]) if HorV == 'V': ow, oh = 40+rows*usize[quality][0]+(rows-1)*10, 40+cols*usize[quality][0] else: ow, oh = 40+cols*usize[quality][0], 40+rows*usize[quality][0]+(rows-1)*10 out_im = Image.new('RGBA', (ow, oh), '#f0f0f0') for row in range(rows): if HorV == 'V': row_im = Image.new('RGBA', (usize[quality][0], cols*usize[quality][0]), 'white') offset = (ow-(usize[quality][0]+10)*(row+1)-10, 20) else: row_im = Image.new('RGBA', (cols*usize[quality][0], usize[quality][0]), 'white') offset = (20, 20+(usize[quality][0]+10)*row) for col, ch in enumerate(text_list[row]): if HorV == 'V': pos = (0, col*usize[quality][0]) else: pos = (col*usize[quality][0],0) ch_im = get_word(ch, quality) row_im.paste(bg_im, pos) row_im.paste(ch_im, (pos[0]+usize[quality][1], pos[1]+usize[quality][1]), mask=ch_im) out_im.paste(row_im, offset) if out_file: out_im.convert('RGB').save(out_file) out_im.show()
⑤,成品展示
完整代碼奉上,需要先安裝需要的python庫。
import io from PIL import Image import requests def get_word(ch, quality): fp = io.BytesIO(requests.post(url='http://xufive.sdysit.com/tk', data={'ch':ch}).content) im = Image.open(fp) w, h = im.size if quality == 'M': w, h = int(w*0.75), int(0.75*h) elif quality == 'L': w, h = int(w*0.5), int(0.5*h) return im.resize((w,h)) def get_bg(quality): return get_word('bg', quality) def write_couplets(text, HorV='V', quality='L', out_file=None): usize = {'H':(640,23), 'M':(480,18), 'L':(320,12)} bg_im = get_bg(quality) text_list = [list(item) for item in text.split()] rows = len(text_list) cols = max([len(item) for item in text_list]) if HorV == 'V': ow, oh = 40+rows*usize[quality][0]+(rows-1)*10, 40+cols*usize[quality][0] else: ow, oh = 40+cols*usize[quality][0], 40+rows*usize[quality][0]+(rows-1)*10 out_im = Image.new('RGBA', (ow, oh), '#f0f0f0') for row in range(rows): if HorV == 'V': row_im = Image.new('RGBA', (usize[quality][0], cols*usize[quality][0]), 'white') offset = (ow-(usize[quality][0]+10)*(row+1)-10, 20) else: row_im = Image.new('RGBA', (cols*usize[quality][0], usize[quality][0]), 'white') offset = (20, 20+(usize[quality][0]+10)*row) for col, ch in enumerate(text_list[row]): if HorV == 'V': pos = (0, col*usize[quality][0]) else: pos = (col*usize[quality][0],0) ch_im = get_word(ch, quality) row_im.paste(bg_im, pos) row_im.paste(ch_im, (pos[0]+usize[quality][1], pos[1]+usize[quality][1]), mask=ch_im) out_im.paste(row_im, offset) if out_file: out_im.convert('RGB').save(out_file) out_im.show() text = '福' write_couplets(text, HorV='V', quality='M', out_file='福.jpg')
到此,關于“怎么用python寫一個福字”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。