python实现jpg图片批量转pdf
背景
工作中遇到一个问题,同事整理资料比较麻烦,希望把一个文件夹下的jpg 图片批量转到一个pdf上,避免自己一张一张向pdf插入word,这里用python实现了一下。同时支持多个文件夹,每个文件夹下的照片生成一个pdf,不多说了,上代码:
代码
from PIL import Image
import os
from fpdf import FPDF
import PIL.ImageOpsdef exif_transpose(img):if not img:return imgexif_orientation_tag = 274if hasattr(img, "_getexif") and isinstance(img._getexif(), dict) and exif_orientation_tag in img._getexif():exif_data = img._getexif()orientation = exif_data[exif_orientation_tag]print("方向")print(orientation)# Handle EXIF Orientationif orientation == 1:# Normal image - nothing to do!passelif orientation == 2:# Mirrored left to rightimg = img.transpose(PIL.Image.FLIP_LEFT_RIGHT)elif orientation == 3:# Rotated 180 degreesimg = img.rotate(180)elif orientation == 4:# Mirrored top to bottomimg = img.rotate(180).transpose(PIL.Image.FLIP_LEFT_RIGHT)elif orientation == 5:# Mirrored along top-left diagonalimg = img.rotate(-90, expand=True).transpose(PIL.Image.FLIP_LEFT_RIGHT)elif orientation == 6:# Rotated 90 degreesimg = img.rotate(-90, expand=True)elif orientation == 7:# Mirrored along top-right diagonalimg = img.rotate(90, expand=True).transpose(PIL.Image.FLIP_LEFT_RIGHT)elif orientation == 8:# Rotated 270 degreesimg = img.rotate(90, expand=True)return imgdef load_image_file(file):# Load the image with PILimg = PIL.Image.open(file)if hasattr(PIL.ImageOps, 'exif_transpose'):# Very recent versions of PIL can do exit transpose internallyimg = PIL.ImageOps.exif_transpose(img)else:# Otherwise, do the exif transpose ourselvesimg = exif_transpose(img)return imgdef getFileList(dir, Filelist, ext=None):"""获取文件夹及其子文件夹中文件列表输入 dir:文件夹根目录输入 ext: 扩展名返回: 文件路径列表"""newDir = dirif os.path.isfile(dir):if ext is None:Filelist.append(dir)else:if "jpg" in dir[-3:] or "JPG" in dir[-3:]:Filelist.append(dir)elif os.path.isdir(dir):for s in os.listdir(dir):newDir = os.path.join(dir, s)getFileList(newDir, Filelist, ext)return Filelistdef makePdf(pdfName, dir): # listPages,以列表形式传入,方便批量合成listimage = getFileList(dir, [], "jpg")pdf = FPDF(orientation='P', unit='mm', format='A4')for page in listimage:cover = Image.open(page)pdf.add_page(orientation='P')# 先判断图片是否有exif信息# 如果有则旋转图片,针对图片方向读取不正确的问题new_img = load_image_file(page)width, height = cover.sizenew_img.save(page)# 根据图片比例判断是适配高度还是宽度# 根据A4纸比例计算if width / height > 210 / 297:pdf.image(page, x=0, y=0, w=210)else:left = (210 - 297 / height * width) / 2pdf.image(page, x=left, y=0, h=297)# 保存pdfpdf.output(pdfName, "F")if __name__ == '__main__':current_path = os.getcwd()print("当前目录" + current_path)file_list = os.listdir(".") # .代表根目录下# 当前目录下,每个文件夹内的图片单独生成一个pdffor name in file_list:path = os.path.join(current_path, name)print("遍历文件夹" + path)if os.path.isdir(path):print("生成pdf" + path)makePdf(name + ".pdf", path)else:print("跳过" + path)print("执行完成")
结果
python实现jpg图片批量转pdf相关推荐
- 图片批量转换pdf文件
文章目录 1. 依赖 2. 工具类抽取 1. 依赖 <!--图片转换pdf Start--><!-- https://mvnrepository.com/artifact/com.i ...
- Python办公自动化(三)|批量合并PDF
分享一个实用的办公脚本:将多个PDF合并为一个PDF,例如我手上现在有如下3个PDF分册,需要整合成一个完整的PDF 如果换成你操作的话,是不是打开百度搜索:PDF合并,然后去第三方网站操作,可能会收 ...
- Python:对图片批量进行重命名
Python:对图片批量进行重命名 参考链接:https://www.cnblogs.com/znh8/p/11809737.html 将视频转化为图片,以帧的形式进行命名, 如 0.jpg.1.jp ...
- 怎么把JPG转换成PDF格式?图片批量转换PDF的简单方法
文件格式的转换在我们的工作中扮演了非常重要的角色,比如怎么把JPG转换成PDF格式?如果可以轻松的解决文件格式转换问题,相信大家的工作效率也会提高很多.如果有小伙伴不知道如何转换,那么小编分享一个不用 ...
- 手把手 | 20行Python代码教你批量将PDF转为Word
2019独角兽企业重金招聘Python工程师标准>>> 在日常工作或学习中,经常会遇到这样的无奈: "小任,你把这个PDF中的文件码出来发我" 艹,倒霉,2M的P ...
- python实现pdf转word详解_手把手|20行Python代码教你批量将PDF文件转为Word格式(包教包会)...
在日常工作或学习中,经常会遇到这样的无奈: "小任,你把这个PDF中的文件码出来发我" 倒霉,2M的PDF12点也完不了啊! 很多时候在学习时发现许多文档都是PDF格式,PDF格式 ...
- python把pdf转word_手把手|20行Python代码教你批量将PDF文件转为Word格式(包教包会)...
作者:菜鸟分析 Python爱好者社区--专栏作者 个人介绍:一个痴迷于Python语言的业余程序猿,未来的理想是能够与一群痴迷于Python语言的程序猿改变世界 知乎专栏: https://zhua ...
- 手机中的图片批量转PDF的操作方法
之前手机最大的用处就是打电话,现在新增了一个必不可少的功能就是拍照,吃饭拍购物拍生活拍各种拍就造成手机中照片过多,进而出现手机反应速度变迟钝等各种 操作不便的情况,那要怎样操作能减缓手机的压力呢?这时 ...
- 基于Python的图片批量转PDF实现
不多BB,直接上代码: from reportlab.pdfgen import canvas from reportlab.lib.units import inch, cm from report ...
最新文章
- 首席信息官利用AI提升自身地位的三种方法
- Youtube推荐系统是如何挖掘用户内心另一面的
- python强制结束函数_为什么Python没有 main 函数?终于有人给出了正确答案!
- java中soo_soo_ - SegmentFault 思否
- VCSA 6.7 U3部署(vCenter Server Appliance(VCSA)6.7 U3部署指南)
- eNSP中AC用Web方式登录
- string services
- jquery元素插入、删除、清空
- 双纵坐标的绘图命令_Matplotlib绘图 | 快速定义图表样式的小技巧
- 自动机器学习工具全景图:精选22种框架,解放炼丹师
- SQL语句中 NOT IN 子句的“正确打开方式”
- Window平台编译log4cpp使用方法记录 (二)
- 亮度,对比度,饱和度,锐度对最终图像效果的影响
- Hostapd.conf详细释义
- UVA 10306 e-Coins (二维背包)
- conenct的用法(2种方法)
- 使用MPU6050在STM32F103C8T6中的硬件I2C数据传输
- glob.glob() in Python
- Linux 命令xxd输出十六进制
- 阿里Java面试必问:java多线程实例
热门文章
- java 验证码突然不显示
- phpstorm 配置 xdebug断点调试
- 聊天室程序python_Python聊天室程序(基础版)_python
- 274. H 指数(中)
- 名编辑电子杂志大师教程 | 主页按钮
- Makefile自动搜所目录及子目录下的的所有文件、自动生成编译目录
- PTA7-2 建国的数学难题(C语言)(递归方法)
- 说说计算机有哪些用途,电脑对你有什么用处?写5个英语句子,说明电脑对你的重要性...
- python模拟登录163邮箱_python模拟登陆163邮箱并下载邮件内容(第三版代码片段)
- filter过滤器的FilterConfig 类