import os
import math
import cairosvg
temp_image = os.path.join(fileDir, "splash.png")
# PIL 本身不具备抗锯齿功能,输出的图片锯齿严重,我们进行四倍超采样,然后缩放的方式进行抗锯齿操作
scale = 4.0
# background_color='#FFFFFF'
# 注意如果不设置DPI,默认dpi是96 人眼低于326dpi的可见明显锯齿
cairosvg.svg2png(url=raw, write_to=temp_image, output_width=math.ceil(image_width*scale), dpi=400,
output_height=math.ceil(image_height*scale))
from PIL import Image
bg = Image.new("RGBA", (math.ceil(output_width*scale), math.ceil(output_height*scale)))
fg = Image.open(temp_image)
bg.paste(fg, (math.ceil((output_width - image_width)*scale/2),
math.ceil((output_height - image_height)*scale/2)), fg)
bg.resize((output_width, output_height), Image.ANTIALIAS);
bg.save(out)
os.remove(temp_image)