def make_a_snapshot(file_name, output_name, delay=DEFAULT_DELAY):
file_type = output_name.split('.')[-1]
pixel_ratio = 2
shell_flag = False
if sys.platform == 'win32':
shell_flag = True
__actual_delay_in_ms = int(delay * 1000)
# add shell=True and it works on Windows now.
proc_params = [
PHANTOMJS_EXE,
os.path.join(get_resource_dir('phantomjs'), 'snapshot.js'),
file_name.replace('\\', '/'),
file_type,
str(__actual_delay_in_ms),
str(pixel_ratio)
]
proc = subprocess.Popen(
proc_params, stdout=subprocess.PIPE, shell=shell_flag)
if PY2:
content = proc.stdout.read()
content = content.decode('utf-8')
else:
content = io.TextIOWrapper(proc.stdout, encoding="utf-8").read()
content_array = content.split(',')
if len(content_array) != 2:
raise Exception("No snapshot taken by phantomjs. " +
"Please make sure it is installed " +
"and available on your path")
base64_imagedata = content_array[1]
imagedata = decode_base64(base64_imagedata.encode('utf-8'))
if file_type in ['pdf', 'gif']:
save_as(imagedata, output_name, file_type)
elif file_type in ['png', 'jpeg']:
save_as_png(imagedata, output_name)
else:
raise Exception(NOT_SUPPORTED_FILE_TYPE % file_type)
评论列表
文章目录