不多BB,直接上代码:

from reportlab.pdfgen import canvas
from reportlab.lib.units import inch, cm
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Image, PageBreak
from reportlab.lib.pagesizes import A4,A3,A2,A1, legal, landscape
from reportlab.lib.utils import ImageReader
import PIL.Image,PIL.ExifTags
from os import listdir
import os,re
import time
from reportlab.lib.units import inchdef img_search(mypath, filenames):for lists in os.listdir(mypath):path = os.path.join(mypath,lists)if os.path.isfile(path):expression = r'[\w]+\.(jpg|png|jpeg)$'if re.search(expression, path, re.IGNORECASE):filenames.append(path)elif os.path.isdir(path):img_search(path, filenames)# 加入了文件名排序
def img_search1(mypath, filenames):fn = os.listdir(mypath)# 切割文件名下标24:-4的内容,并转int类型进行升序排序fn.sort(key=lambda x:int(x[24:-4]))for lists in fn:path = os.path.join(mypath,lists)if os.path.isfile(path):a = path.split('.')if a[-1] in ['jpg', 'png', 'JPEG']:filenames.append(path)elif os.path.isdir(path):img_search1(path,filenames)def rotate_img_to_proper(image):try:# image = Image.open(filename)if hasattr(image, '_getexif'):  # only present in JPEGsfor orientation in PIL.ExifTags.TAGS.keys():if PIL.ExifTags.TAGS[orientation] == 'Orientation':breake = image._getexif()  # returns None if no EXIF dataif e is not None:#log.info('EXIF data found: %r', e)exif = dict(e.items())orientation = exif[orientation]# print('found, ',orientation)if orientation == 3:image = image.transpose(Image.ROTATE_180)elif orientation == 6:image = image.transpose(Image.ROTATE_270)elif orientation == 8:image = image.rotate(90,expand=True)except:passreturn imagedef main(output_file_name,src_folder=None):# output_file_name = 'out.pdf'#save_file_name = 'ex.pdf'#doc = SimpleDocTemplate(save_file_name, pagesize=A1,#                     rightMargin=72, leftMargin=72,#                     topMargin=72, bottomMargin=18)imgDoc = canvas.Canvas(output_file_name)#pagesize=letterimgDoc.setPageSize((2333,1313))document_width,document_height = 2333,1313if src_folder is None: mypath = input('Input the image folder please:')else: mypath = src_folderfilenames=[]start = time.time()img_search1(mypath, filenames)end = time.time()print('find file cost time: ', end-start, 'find files: ', len(filenames))# for f in filenames:#     print(f)for image in filenames:try:image_file = PIL.Image.open(image)image_file = rotate_img_to_proper(image_file)image_width, image_height = image_file.sizeprint('img size:', image_file.size)if not(image_width>0 and image_height>0):raise Exceptionimage_aspect = image_height/float(image_width)#Determins the demensions of the image in the overviewprint_width  = document_widthprint_height = document_width*image_aspectimgDoc.drawImage(ImageReader(image_file),document_width-print_width,document_height-print_height,width=print_width,height=print_height,preserveAspectRatio=True)#inform the reportlab we want a new pageimgDoc.showPage()except Exception as e:print('error:',e,image)imgDoc.save()print('Done')if __name__ == '__main__':main(output_file_name="E:\File\my\转出.pdf", src_folder="E:\File\my\images")

基于Python的图片批量转PDF实现相关推荐

  1. 【Python+图片切割+图片合并】基于Python的图片批量切割与合并(保持原像素不变,不会出现像素大小不匹配、填充黑边的问题)

    基于Python的图片批量切割与合并(保持原像素不变,不会出现像素大小不匹配.填充黑边的问题) 前言 效果图 1.批量读取文件 2.清空目标目录方法(配合切割图片方法使用) 3.批量切割图片(像素不足 ...

  2. 基于python的图片批量添加水印和重命名程序

    开发环境要求 本系统的软件开发及运行环境具体如下. 操作系统:Windows 7.Windows 10. Python版本:Python 3.7.1. 开发工具:PyCharm 2018. Pytho ...

  3. 基于Python使用ffmpeg批量缩放图片

    基于Python使用ffmpeg批量缩放图片 一.前言 ​ 笔者因为项目原因或者个人撰文需要,经常要写大量技术文档,文档中通常需要配图,但配图就有一个比较讨厌的问题:截图大小不一,宽度通常要手动调整, ...

  4. 基于Python实现图片格式转换的小程序

    基于Python实现图片格式转换的小程序 特点: 批量处理图片 转换常见的4种图片格式 运行窗口 运行窗口-1 选择图片(可批量选择)-2 假设选中4张JEPG格式的图片 格式选择窗口-3 假设选择目 ...

  5. 图片批量转换pdf文件

    文章目录 1. 依赖 2. 工具类抽取 1. 依赖 <!--图片转换pdf Start--><!-- https://mvnrepository.com/artifact/com.i ...

  6. python空间数据处理_基于Python的空间数据批量处理方法

    第 34 卷 第 4 期 2011 年 8 月 测绘与空间地理信息 GEOMATICS & SPATIAL INFORMATION TECHNOLOGY Vol. 34,No. 4 Aug. ...

  7. 基于Python的文件批量重命名(附详细过程解读)

    基于Python的文件批量重命名(附详细过程解读) 0.闲言碎语 hi ~ 欢迎关注公众号"壹贰叁言"! 啥都写,图一乐哈哈! 1.前因后果 前几日,我在用arcgis处理一些数据 ...

  8. Python 给图片批量加logo+logo透明化

    利用Python 给图片批量加logo+logo透明化 通过图片上新建图层 把logo水印图片添加到图层 并合并 from PIL import ImageDraw from PIL import I ...

  9. 怎么把JPG转换成PDF格式?图片批量转换PDF的简单方法

    文件格式的转换在我们的工作中扮演了非常重要的角色,比如怎么把JPG转换成PDF格式?如果可以轻松的解决文件格式转换问题,相信大家的工作效率也会提高很多.如果有小伙伴不知道如何转换,那么小编分享一个不用 ...

最新文章

  1. 性能测试总结(二)---测试流程篇
  2. nginx tomcat 负载
  3. curl命令java_让 Bug 无处藏身,Java 线上问题排查思路、常用工具
  4. 帮助学生改善学习方法_学生应该如何花费时间改善自己的幸福
  5. js读取服务器上的txt文件,javascript – 每15秒读取一次文本文件的内容
  6. 完整的可按年份和月份查询数据并显示
  7. 《Python编程从入门到实践》记录之第2章 变量和简单数据类型总结(思维导图)
  8. 异常处理关于数组java_关于java异常处理的自我学习
  9. 谈谈python enumerate()函数的用法_python enumerate函数的使用方法总结
  10. 深入浅出Python机器学习2——基于Python语言的环境配置
  11. win10的当前桌面壁纸保存位置
  12. 网络直播延迟该如何解决这个问题
  13. phpnow安装教程
  14. 如何知道PDF是不是正常的A4大小尺寸?
  15. pyMuPDF How To
  16. bochs运行xp_简单教程:Bochsxp镜像超级简化版安装教程
  17. Android端记录跑步运动轨迹数据的App
  18. 第十七届中国计量大学程序设计竞赛 D Dessert Time
  19. commbean java_Oracle Java 7 JmxMBeanServer类远程代码执行漏洞
  20. Python:统计文件中所有英文单词的数量

热门文章

  1. php1.6t发动机机油选择,1.6t用什么机油选择啊!?
  2. 毕业设计 基于JSP动漫论坛的设计与实现
  3. 1.项目介绍——毕业设计之论坛项目
  4. gdpr通用数据保护条例_关于通用数据保护法规(GDPR),您需要了解的15件事
  5. 杂篇-01-Unity中创建Mesh时遇到的问题
  6. pmp证书报考流程+pmp备考+pmp学习干货+pmp指南汇总
  7. 数据库范式5nf_第五范式(5NF)| 数据库管理系统
  8. 最优化算法学习笔记+个人总结(一)
  9. AnyRTC将携互动直播连线2016杭州·云栖大会
  10. mysql group by个数_mysql 统计 group by 之后的 group 的个数