因?yàn)楣ぷ餍枰筛鞣N大小的圖片,所以寫(xiě)了個(gè)小腳本,順便支持了下圖畫(huà)文字內(nèi)容。

具體代碼如下:
from PIL import Image, ImageDraw, ImageFont
'''
Auth: Xiaowu Chen
Note: Please install [pillow] library before run this script.
'''
def draw_image(new_img, text, show_image=False):
text = str(text)
draw = ImageDraw.Draw(new_img)
img_size = new_img.size
draw.line((0, 0) + img_size, fill=128)
draw.line((0, img_size[1], img_size[0], 0), fill=128)
font_size = 40
fnt = ImageFont.truetype('arial.ttf', font_size)
fnt_size = fnt.getsize(text)
while fnt_size[0] > img_size[0] or fnt_size[0] > img_size[0]:
font_size -= 5
fnt = ImageFont.truetype('arial.ttf', font_size)
fnt_size = fnt.getsize(text)
x = (img_size[0] - fnt_size[0]) / 2
y = (img_size[1] - fnt_size[1]) / 2
draw.text((x, y), text, font=fnt, fill=(255, 0, 0))
if show_image:
new_img.show()
del draw
def new_image(width, height, text='default', color=(100, 100, 100, 255), show_image=False):
new_img = Image.new('RGBA', (int(width), int(height)), color)
draw_image(new_img, text, show_image)
new_img.save(r'%s_%s_%s.png' % (width, height, text))
del new_img
def new_image_with_file(fn):
with open(fn, encoding='utf-8') as f:
for l in f:
l = l.strip()
if l:
ls = l.split(',')
if '#' == l[0] or len(ls) < 2:
continue
new_image(*ls)
if '__main__' == __name__:
new_image(400, 300, 'hello world any size', show_image=True)
# new_image_with_file('image_data.txt')
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前名稱:Python批量生成特定尺寸圖片及圖畫(huà)任意文字的實(shí)例-創(chuàng)新互聯(lián)
當(dāng)前路徑:http://www.yijiale78.com/article14/dodege.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開(kāi)發(fā)、ChatGPT、云服務(wù)器、品牌網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)公司、營(yíng)銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容