国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python批量生成特定尺寸圖片及圖畫任意文字的實(shí)例

2020-02-16 00:59:50
字體:
供稿:網(wǎng)友

因?yàn)楣ぷ餍枰筛鞣N大小的圖片,所以寫了個(gè)小腳本,順便支持了下圖畫文字內(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')  

如果你需要批量的話,批量數(shù)據(jù)文件的格式如下:

#width,height,text200,200,hello300,255,world

執(zhí)行后的效果如下:

Python批量生成特定尺寸圖片及圖畫任意文字

以上這篇Python批量生成特定尺寸圖片及圖畫任意文字的實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持武林站長站。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 青河县| 汽车| 涿鹿县| 英超| 横峰县| 四会市| 南昌县| 利津县| 牟定县| 湖南省| 昭平县| 鄂州市| 随州市| 茶陵县| 晋江市| 望谟县| 镇雄县| 新闻| 个旧市| 霍邱县| 上栗县| 突泉县| 工布江达县| 科技| 化隆| 秦安县| 新晃| 金溪县| 皋兰县| 谢通门县| 吉首市| 大关县| 凭祥市| 天柱县| 社旗县| 晋中市| 黄浦区| 潼关县| 长治县| 曲水县| 香河县|