将文字转化为图片(竖排)

作用:将文字转化为图片保存

#载入必要的模块
import pygame
import os
from PIL import Image
import sys


def getPic(title,width_i = 200,height_i = 200,isWordSplited="0",isWordMarginMin="1"):
    # 待转换文字title
    # 图片压缩后的大小width_i = 200,height_i = 200
    # 英文单词是否单个显示isWordSplited 
    # 是否将字符间间距调到最小isWordMarginMin

    width_i=int(width_i)
    height_i=int(height_i)
    #pygame初始化
    pygame.init()
    result=[]
    tmpstr=""
    if isWordSplited=="1":
        #对文字进行切分(英语单词字母分开)
        for s in title:
            result.append(s)
    else:
        #对文字进行切分(英语单词作为整体)
        for s in title:
            if ord(s) in range(ord('A'),ord('Z')) or ord(s) in range(ord('a'),ord('z')):
                tmpstr=tmpstr+s
            else:
                if(tmpstr!=""):
                    result.append(tmpstr)
                    tmpstr=""
                result.append(s)

    print(result)
    print(width_i)


    wordcount=len(result)
    for i in range(0,wordcount):
        #设置字体和字号
        font = pygame.font.Font("msyh.ttc", 64)

        #渲染图片,设置背景颜色和字体样式,前面的颜色是字体颜色
        ftext = font.render(result[i], True, (65, 83, 130),(255, 255, 255),)
        #ftext = pygame.transform.rotate(ftext,90)
        #保存图片
        pygame.image.save(ftext, str(i)+".jpg")#图片保存地址




    #每行每列显示图片数量
    line_max = 1
    row_max = wordcount

    #参数初始化
    num = 0
    pic_max=line_max*row_max
    #生成背景图片
    toImage = Image.new('RGB',(width_i*line_max,height_i*row_max),color=(255,255,255))
    totalHeight=0
    for i in range(0,row_max): 
        for j in range(0,line_max):
            pic_fole_head =  Image.open(str(num)+".jpg")
            width,height =  pic_fole_head.size
            scale=1
            #按照一定比例拉伸图片
            if width>height:
                scale=width_i/width
            else:
                scale=height_i/height
            
            
            tmppic = pic_fole_head.resize((int(width*scale),int(height*scale)))
            totalHeight=int(height*scale)+totalHeight
            #计算每个字在新图中的位置
            loc = (int(j%line_max*width_i+(width_i-width*scale)/2),int(i%row_max*height_i))
            #将生成的图片粘贴到大图中
            toImage.paste(tmppic,loc)
            num= num+1
        if num >= pic_max:
            break
    
    #按照每个单词或者字的实际高度进行重新排列
    if isWordMarginMin=="1":
        currentHeight=0
        toImageSorted = Image.new('RGB',(width_i*line_max,totalHeight),color=(255,255,255))
        #重新排列
        num=0
        for i in range(0,row_max): 
            for j in range(0,line_max):
                pic_fole_head =  Image.open(str(num)+".jpg")
                width,height =  pic_fole_head.size
                scale=1
                #按照一定比例拉伸图片
                if width>height:
                    scale=width_i/width
                else:
                    scale=height_i/height
                
                tmppic = pic_fole_head.resize((int(width*scale),int(height*scale)))
                
                #计算每个字在新图中的位置
                loc = (int(j%line_max*width_i+(width_i-width*scale)/2),currentHeight)
                currentHeight=int(height*scale)+currentHeight
                #将生成的图片粘贴到大图中
                toImageSorted.paste(tmppic,loc)
                num= num+1
            if num >= pic_max:
                break
        print(toImageSorted.size)
        toImageSorted.save(title+'.png')
    else:
        print(toImage.size)
        toImage.save(title+'.png')
    for i in range(0,row_max): 
        os.remove(str(i)+".jpg")

#读取命令行参数
title=sys.argv[1]
width_i=sys.argv[2]
height_i=sys.argv[3]
isWordSplited=sys.argv[4]
isWordMarginMin=sys.argv[5]
getPic(title,width_i,height_i,isWordSplited,isWordMarginMin)

主要参考:

https://blog.csdn.net/johinieli/article/details/76151247

https://www.cnblogs.com/asreg/p/6791406.html

 代码和字体文件:https://download.csdn.net/download/heartwasd95/10712913

猜你喜欢

转载自www.cnblogs.com/littleby/p/9770763.html
今日推荐