LaTeX output to image

本文介绍了一种使用python脚本将PDF格式的图像转换为PNG格式的方法,以便于在Word或PPT等软件中使用。通过安装pdf2image库并设置参数,如输入文件、分辨率、输出路径和图片名称,可以实现PDF到图片的高效转换。
部署运行你感兴趣的模型镜像

tikz画图输出是pdf格式,但是这种格式的图片,只有LaTeX可以插入到文章中,如果是word或者PPT都是无法插入这种图片的。

解决方案

写一个pdf转图片的脚本

环境要求

  • pdf2image
pip3 install pdf2image
  • pdfTools
sudo apt-get install libpoppler-cpp-dev

pdfTools的更多信息

pdf2img.py

import tempfile
import argparse
from pdf2image import convert_from_path,convert_from_bytes

parser = argparse.ArgumentParser(description='pdf2image')
parser.add_argument('--inputfile', '-i', help='input your pdf file',required=True)
parser.add_argument('--dpi', '-d', help='input your image dpi',default=400)
parser.add_argument('--outfolder', '-f', help='set your image\'s path', required=True)
parser.add_argument('--outfile', '-o', help='set your image\'s name', default="out")
args = parser.parse_args()

with tempfile.TemporaryDirectory() as path:
    image_from_path = convert_from_path(args.inputfile, dpi=args.dpi, fmt='png',output_folder=args.outfolder,output_file=args.outfile)
pdf2img.py 使用示例
  • 参数说明

--inputfile / -i:你的pdf文件

--dpi / -d:你需要的图片分辨率,默认是400

--outfolder / -f:你图片的输出路径

--outfile / -o:你的图片名

python3 pdf2img.py -i example.pdf -d 400 -f . -o example

与vim交互

autocmd filetype tex noremap <buffer> <LEADER>i :!python3 ~/scripts/pdf2img.py -i %<.pdf -f %:h -o %<<CR>

这里没有给dpi参数,直接按照默认的分辨率生成图片。

在你编译完tex文件生成pdf以后,按下<leader>+i 会自动调用pdf2img.py脚本将pdf转为png格式的图片,这里我将pdf2img.py放在了我scripts文件夹下,可以根据需要改成自己的路径。

我的其他脚本参见这里

您可能感兴趣的与本文相关的镜像

Python3.9

Python3.9

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值