Skip to content

Instantly share code, notes, and snippets.

@Sg4Dylan
Last active December 4, 2017 11:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sg4Dylan/f950c195e503df118a8585c62778a61f to your computer and use it in GitHub Desktop.
Save Sg4Dylan/f950c195e503df118a8585c62778a61f to your computer and use it in GitHub Desktop.
自动糊海报工具及配置文件
#!/usr/bin/env python
#coding:utf-8
# Author: Sg4Dylan --<sg4dylan#gmail.com>
# Created: 7/14/2017
from PIL import Image,ImageFont,ImageDraw
import render_config as config
def auto_poster(text: dict, param: dict):
# load text
inner_text = ""
# load image
image_layer = Image.open(param["image_path"]).convert('RGBA')
text_layer = Image.new('RGBA', image_layer.size, (255,255,255,param["font_alpha"]))
text_draw = ImageDraw.Draw(text_layer)
# font setting
render_font_size = 1
render_font = ImageFont.truetype(param["font_path"], render_font_size)
# render size
img_w, img_h = image_layer.size
font_w, font_h = 0, 0
def load_text():
nonlocal inner_text
for i in text:
inner_text += i + text[i]
def estimate_font_size():
nonlocal text_draw, render_font_size, render_font, font_w, font_h
# renew font setting
render_font = ImageFont.truetype(param["font_path"], render_font_size)
# measure text block size
font_w, font_h = text_draw.textsize(inner_text,render_font)
# estimate font size
ratio = config.param["render_ratio"]
# print process
# print("Size: %d x %d" % (font_w, font_h))
if font_w <= img_w/ratio and font_h <= img_h/ratio:
render_font_size += 1
estimate_font_size()
def render_position():
position_dict = {
0 : (0, 0),
1 : (img_w-font_w, 0),
2 : (0, img_h-font_h),
3 : (img_w-font_w, img_h-font_h)
}
postion_result = position_dict.get(param["render_position"], ((img_w-font_w)/2, (img_h-font_h)/2))
postion_result = (postion_result[0]+param["render_postion_offset"][0],postion_result[1]+param["render_postion_offset"][1])
return postion_result
def render_align():
position_align = {
0 : "left",
1 : "right",
2 : "left",
3 : "right",
}
position_align = position_align.get(param["render_position"], "center")
return position_align
def render_info():
info = "Render Info: \n"
info += "input image = %d x %d\n" % (img_w, img_h)
info += "render font = %d x %d\n" % (font_w, font_h)
info += "render ratio = %.4f - %.4f\n" % (font_w/img_w,font_h/img_h)
info += "font size = %d\n" % (render_font_size)
info += "font color = %s\n" % str(param["font_color"])
info += "font layer alpha = %d" % param["font_alpha"]
return info
def render_poster():
load_text()
estimate_font_size()
text_draw.multiline_text(
render_position(),inner_text,param["font_color"],
render_font,align=render_align())
render_result = Image.alpha_composite(image_layer, text_layer)
render_result.save(param["poster_path"]+"."+param["poster_format"].lower(), param["poster_format"], quality=100)
return render_info()
return render_poster()
print(auto_poster(config.text, config.param))
# 这个地方写每一行的东西
# 注意每一行后边要有逗号
# 中间要有冒号,用 \n 换行
text = {
"歌词:": "XX XX XX XXX\n",
"翻译:": "XX XXX ",
"校对:": "xxxx \n",
"时轴:": "xxxx ",
"特效:": "xxxxx\n",
"后期:": "xxx\n",
"压制:": "xxx"
}
param = {
# 原图路径
"image_path": "input.jpg",
# 出图路径
"poster_path": "output",
# 出图格式
"poster_format": "PNG",
# 字体位置
"font_path": "XHei_Microsoft.TTC",
# 颜色 RGBA
"font_color": (0,0,255,100),
# 文字图层透明度 A
"font_alpha": 0,
# 渲染位置 0-3 为从从左往右从上往下四个角,其他的数字默认为居中
"render_position": 1,
# 比例大小,即渲染出的字块长宽不超过原图的几分之几,比如默认三分之一
"render_ratio": 2,
# 在固定位置的坐标偏移
"render_postion_offset": (-5 ,5)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment