def html_to_png(html):
# Use a temp file in the current working directory so that wkhtmltoimage handles relative URLs properly
temp_file = '.temp.html'
with open(temp_file, 'w') as f:
f.write(html.encode('utf-8'))
command = ['wkhtmltoimage']
if not find_executable(command[0]):
raise ImportError('%s not found' % command[0])
command += ['-f', 'png'] # format output as PNG
command += ['--zoom', '2'] # retina image
command += ['--width', '750'] # viewport 750px wide
command += [temp_file] # read from stdin
command += ['-'] # write to stdout
wkhtml_process = Popen(command, stdout=PIPE, stderr=PIPE)
(output, err) = wkhtml_process.communicate()
os.remove(temp_file)
image = Image.open(BytesIO(output))
image = set_transparent_pixel(image)
return image
评论列表
文章目录