第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何找到正確的圖像尺寸

如何找到正確的圖像尺寸

SMILET 2023-07-11 14:46:36
我正在嘗試用 Python 將文本轉(zhuǎn)換為圖像這是代碼:只要文本是單行,例如“要在 img 1234567890 上寫入的文本”沒事但如果文本包含“\n”,則圖像剪輯和尺寸計算將變得不正確“要寫在 \n img 1234567890 上的文本”請幫忙import numpy as npimport timeimport text_to_imagefrom PIL import Image, ImageDraw, ImageFontimport osfrom win32api import GetSystemMetricsdef text_on_img(filename='01.png', text="Text to write on \n img 1234567890", size=200, color=(0,0,0), bg='white'):    "Draw a text on an Image, saves it, show it"    fnt = ImageFont.truetype('arial.ttf', size)    # create image    image = Image.new(mode = "RGB", size = (int(size/2)*len(text),size+50), color = bg)    draw = ImageDraw.Draw(image)    # draw text    draw.text((10,10), text, font=fnt, fill=(0,0,0))    # save file    image.save(filename)    # show file    os.system(filename)text_on_img()
查看完整描述

2 回答

?
一只斗牛犬

TA貢獻1784條經(jīng)驗 獲得超2個贊

我完美地修復(fù)了它。請測試一下。


import os


from PIL import Image, ImageDraw, ImageFont



def text_on_img(filename='01.png', text="Text to write on \n img 1234567890", size=200, color=(0, 0, 0), bg='white'):

    "Draw a text on an Image, saves it, show it"

    fnt = ImageFont.truetype('arial.ttf', size)

    # create image


    width = max([int(size/2) * len(line) for line in text.split('\n')])

    height = (size + 50) * len(text.split('\n'))


    image = Image.new(mode="RGB", size=(width, height), color=bg)

    draw = ImageDraw.Draw(image)

    # draw text

    draw.text((10, 10), text, font=fnt, fill=(0, 0, 0))

    # save file

    image.save(filename)

    # show file

    os.system(filename)



text_on_img()

結(jié)果:

http://img1.sycdn.imooc.com//64acfb0700013b3406070144.jpg

查看完整回答
反對 回復(fù) 2023-07-11
?
HUH函數(shù)

TA貢獻1836條經(jīng)驗 獲得超4個贊

一種方法是乘以size出現(xiàn)的次數(shù)\n

n = text.count('\n') + 2# create imageimage = Image.new(mode = "RGB", size = (int(size/2)*len(text),size*n), color = bg)

結(jié)果:


http://img1.sycdn.imooc.com//64acfb1a0001941403310088.jpg

如果從文本中刪除\n,結(jié)果將是:

http://img1.sycdn.imooc.com//64acfb200001893806280059.jpg

其他例子:

http://img1.sycdn.imooc.com//64acfb270001a86104730073.jpg

http://img1.sycdn.imooc.com//64acfb2d0001ab6b01930205.jpg

http://img1.sycdn.imooc.com//64acfb34000119d003450254.jpg

查看完整回答
反對 回復(fù) 2023-07-11
  • 2 回答
  • 0 關(guān)注
  • 161 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號