Python image synthesis is described below applied to an image of FIG.

Recent want to climb at the picture, but found climb down the picture below with a literal interpretation, this does not look aesthetically pleasing, can not think of a literal interpretation of the picture, a picture synthesis, placed below the picture
look at the picture format
Here Insert Picture DescriptionI think to this effect
Here Insert Picture Description
the following description take in the picture above
with reference to the power of God to write the generated images
directly on the code

#文字生成图片,得有固定的宽度和文字字符串
def make_text_image(width,text):
    #450对20号字体 
    fontsize = int(width*20/450)
    # 创建Font对象:
    font = ImageFont.truetype('C:\Windows\Fonts\微软雅黑\msyhl.ttc', fontsize)
    #20号字体
    txt = Image.new('RGB', (100, 100), (255, 255, 255))
    # 创建Draw对象:
    draw = ImageDraw.Draw(txt)
    # 所有文字的段落
    duanluo = ""
    # 宽度总和
    sum_width = 0
    # 几行 一开始设置俩行是因为这个有行间距的,我弄了一下午也没搞清楚行间距怎么算
    #求大神教教我
    line_count = 2
    # 行高
    line_height = 0
    #总长
    duanluoheight = 0
    for char in text:
    	#看下每个字的长和宽
        char_width, height = draw.textsize(char,font=font)
        sum_width += char_width
        if(height > line_height):
            #这个每个字符的高度不一样,找出那个最高的
            line_height = height
        #如果超过了规定的长度那就换行
        if sum_width > width-fontsize*2:
            line_count += 1
            sum_width = 0
            duanluo += '\n'
        #将生成一个带有换行的字符串
        duanluo += char
    #后面再加上一个换行
    if not duanluo.endswith('\n'):
        duanluo += '\n'
    #可以求出段落的高度
    duanluoheight = line_count*line_height
    #生成图片
    image = Image.new('RGB', (width, duanluoheight), (255, 255, 255))
    draw = ImageDraw.Draw(image)
    x, y = char_width/2, 0
    # 输出文字:
    draw.text((x, y), duanluo, font=font, fill=(0,0,0))
    # image.save('code.jpg', 'jpeg')
    return image,duanluoheight

Composite picture looked a lot, the idea is to build a large, then entered, plus two small

Complete code, you have to have the F drive, if not, change it enough

import urllib.request
import os
import json
import requests
import time
import re
from PIL import Image, ImageFont, ImageDraw

#今日囧图的编号,gif编号,视频编号
jinristr = "138872" 

#文字生成图片,得有固定的宽度和文字字符串
def make_text_image(width,text):
    #450对20号字体 
    fontsize = int(width*20/450)
    # 创建Font对象:
    font = ImageFont.truetype('C:\Windows\Fonts\微软雅黑\msyhl.ttc', fontsize)
    #20号字体
    txt = Image.new('RGB', (100, 100), (255, 255, 255))
    # 创建Draw对象:
    draw = ImageDraw.Draw(txt)
    # 所有文字的段落
    duanluo = ""
    # 宽度总和
    sum_width = 0
    # 几行 一开始设置俩行是因为这个有行间距的,我弄了一下午也没搞清楚行间距怎么算
    #求大神教教我
    line_count = 2
    # 行高
    line_height = 0
    #总长
    duanluoheight = 0
    for char in text:
    	#看下每个字的长和宽
        char_width, height = draw.textsize(char,font=font)
        sum_width += char_width
        if(height > line_height):
            #这个每个字符的高度不一样,找出那个最高的
            line_height = height
        #如果超过了规定的长度那就换行
        if sum_width > width-fontsize*2:
            line_count += 1
            sum_width = 0
            duanluo += '\n'
        #将生成一个带有换行的字符串
        duanluo += char
    #后面再加上一个换行
    if not duanluo.endswith('\n'):
        duanluo += '\n'
    #可以求出段落的高度
    duanluoheight = line_count*line_height
    #生成图片
    image = Image.new('RGB', (width, duanluoheight), (255, 255, 255))
    draw = ImageDraw.Draw(image)
    x, y = char_width/2, 0
    # 输出文字:
    draw.text((x, y), duanluo, font=font, fill=(0,0,0))
    # image.save('code.jpg', 'jpeg')
    return image,duanluoheight

#生成文件时要对文件名字做处理
def validateTitle(title):
    rstr = r"[\/\\\:\*\?\"\<\>\|]"  # '/ \ : * ? " < > |'
    new_title = re.sub(rstr, "_", title)  # 替换为下划线
    return new_title
#接口
url = "http://tu.duowan.com/index.php?r=show/getByGallery&gid="
#生成有参数的接口
url = url + jinristr + "&_=" + str(int(round(time.time() * 1000)))
resp=requests.get(url)
dataJson=json.loads(resp.content)
listdaa=dataJson['picInfo']
listdaa.sort(key = lambda x:(int)(x['ding']))
str = ''
#循环找到的数据
for i in listdaa:
    print(i['ding'])
    print(i['add_intro'])
    str = str+i['add_intro']+"\n"
#在电脑中定义存放图片的位置并新建
path1='F:\\多玩爬取图片\\'+dataJson['gallery_title']
if os.path.exists(path1):
    pass
else:
    os.makedirs(path1)
#定义文件的连接
for i in listdaa:
	#后面的文件名应该是随着类型的不同而变化的
    picpath = path1+'\\'+validateTitle(i['add_intro']+'.jpg')
    if not os.path.exists(picpath):
    	#下载
        urllib.request.urlretrieve(i['source'], picpath)
    
#写一个文本文件,里面放着他们的文件名字,这个其实没用的
txtpath = path1 + '\\1.txt'
f = open(txtpath,'w')
f.write(str)
f.close()

#将文件名字进行拼接
for i in listdaa:
    picpath = path1+'\\'+validateTitle(i['add_intro']+'.jpg')
    im = Image.open(picpath)
    immg = make_text_image(im.size[0], i['add_intro'])
    zui = Image.new(im.mode, (im.size[0], im.size[1]+immg[1]))
    zui.paste(im, box=(0,0))
    zui.paste(immg[0],box=(0,im.size[1]))
    zui.save(picpath)



Published 163 original articles · won praise 117 · views 210 000 +

Guess you like

Origin blog.csdn.net/u010095372/article/details/88693999