Python多图片合并PDF的方法

yipeiwu_com5年前Python基础

python多图片合并pdf

起因

一个做美工的朋友需要将多个图片jpg 、png 合并起来,PS操作太慢了所以用了python进行完成这个任务

代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File : 2.py
# @Author: huifer
# @Date : 2018/12/20
from PIL import Image
import os
def rea(pdf_name):
  file_list = os.listdir('.')
  pic_name = []
  im_list = []
  for x in file_list:
    if "jpg" in x or 'png' in x or 'jpeg' in x:
      pic_name.append(x)
  pic_name.sort()
  new_pic = []
  for x in pic_name:
    if "jpg" in x:
      new_pic.append(x)
  for x in pic_name:
    if "png" in x:
      new_pic.append(x)
  print("hec", new_pic)
  im1 = Image.open(new_pic[0])
  new_pic.pop(0)
  for i in new_pic:
    img = Image.open(i)
    # im_list.append(Image.open(i))
    if img.mode == "RGBA":
      img = img.convert('RGB')
      im_list.append(img)
    else:
      im_list.append(img)
  im1.save(pdf_name, "PDF", resolution=100.0, save_all=True, append_images=im_list)
  print("输出文件名称:", pdf_name)
if __name__ == '__main__':
  tttt = """
 _____ _____ _____  _______ ____  _____ _____ ______ 
 | __ \_  _/ ____| |__  __/ __ \ | __ \| __ \| ____|
 | |__) || || |     | | | | | | | |__) | | | | |__  
 | ___/ | || |     | | | | | | | ___/| | | | __| 
 | |  _| || |____   | | | |__| | | |  | |__| | |   
 |_|  |_____\_____|  |_| \____/ |_|  |_____/|_|   
"""
  print(tttt)
  print("合成")
  pdf_name = input("请输入合成PDF文件名称:")
  if ".pdf" in pdf_name:
    rea(pdf_name=pdf_name)
  else:
    rea(pdf_name="{}.pdf".format(pdf_name))
  input("按任意键结束")

合成后

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对【听图阁-专注于Python设计】的支持。如果你想了解更多相关内容请查看下面相关链接

相关文章

OpenCV 边缘检测

OpenCV 边缘检测

边缘在人类视觉和计算机视觉中均起着重要的作用。 人类能够仅凭一张背景剪影或一个草图就识别出物体类型和姿态。 其中OpenCV提供了许多边缘检测滤波函数,这些滤波函数都会将非边缘区域转为黑...

Python version 2.7 required, which was not found in the registry

Python version 2.7 required, which was not found in the registry

安装PIL库的时候,直接提示:Python version 2.7 required, which was not found in the registry。 如图: 大意是说找不到...

Python实现求数列和的方法示例

本文实例讲述了Python实现求数列和的方法。分享给大家供大家参考,具体如下: 问题: 输入 输入数据有多组,每组占一行,由两个整数n(n<10000)和m(m<1000)组...

Python 继承,重写,super()调用父类方法操作示例

Python 继承,重写,super()调用父类方法操作示例

本文实例讲述了Python 继承,重写,super()调用父类方法操作。分享给大家供大家参考,具体如下: demo.py(继承,重写,super): # 父类 class Dog:...

django 按时间范围查询数据库实例代码

从前台中获得时间范围,在django后台处理request中数据,完成format,按照范围调用函数查询数据库。 介绍一个简单的功能,就是从web表单里获取用户指定的时间范围,然后在数据...