最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python3將jpg轉(zhuǎn)為pdf文件的方法示例

 更新時間:2019年12月13日 09:31:34   作者:nudt_qxx  
這篇文章主要介紹了Python3將jpg轉(zhuǎn)為pdf文件的方法,結(jié)合完整實例形式分析了Python3針對jpg轉(zhuǎn)pdf格式的文件讀寫、編碼裝換等相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了Python3將jpg轉(zhuǎn)為pdf文件的方法。分享給大家供大家參考,具體如下:

#coding=utf-8
#!/usr/bin/env python
"""
convert image to pdf file
"""
#Author: mrbeann 
import os
import sys
import glob
import platform
from reportlab.lib.pagesizes import letter, A4, landscape
from reportlab.platypus import SimpleDocTemplate, Image
from reportlab.lib.units import inch
from reportlab.pdfgen import canvas
from reportlab import rl_settings
from PIL import Image
import importlib,sys
#importlib.reload(sys)
#sys.setdefaultencoding("utf-8")
def topdf(path,recursion=None,pictureType=None,sizeMode=None,width=None,height=None,fit=None,save=None):
  """
  Parameters
  ----------
  path : string
      path of the pictures
  recursion : boolean
        None or False for no recursion
        True for recursion to children folder
        wether to recursion or not
  pictureType : list
         type of pictures,for example :jpg,png...
  sizeMode : int
      None or 0 for pdf's pagesize is the biggest of all the pictures
      1 for pdf's pagesize is the min of all the pictures
      2 for pdf's pagesize is the given value of width and height
      to choose how to determine the size of pdf
  width : int
      width of the pdf page
  height : int
      height of the pdf page
  fit : boolean
      None or False for fit the picture size to pagesize
      True for keep the size of the pictures
      wether to keep the picture size or not
  save : string
      path to save the pdf
  """
  if platform.system() == 'Windows':
    path = path.replace('\\','/')
  if path[-1] != '/':
    path = (path + '/')
  if recursion == True:
    for i in os.listdir(path):
      if os.path.isdir(os.path.abspath(os.path.join(path, i))):
        topdf(path+i,recursion,pictureType,sizeMode,width,height,fit,save)
  filelist = []
  if pictureType == None:
    filelist = glob.glob(os.path.join(path, '*.jpg'))
  else:
    for i in pictureType:
      filelist.extend(glob.glob(os.path.join(path, '*.'+i)))
  maxw = 0
  maxh = 0
  if sizeMode == None or sizeMode == 0:
    for i in filelist:
      im = Image.open(i)
      if maxw < im.size[0]:
        maxw = im.size[0]
      if maxh < im.size[1]:
        maxh = im.size[1]
  elif sizeMode == 1:
    maxw = 999999
    maxh = 999999
    for i in filelist:
      im = Image.open(i)
      if maxw > im.size[0]:
        maxw = im.size[0]
      if maxh > im.size[1]:
        maxh = im.size[1]
  else:
    if width == None or height == None:
      raise Exception("no width or height provid")
    maxw = width
    maxh = height
  maxsize = (maxw,maxh)
  if save == None:
    filename_pdf = path + path.split('/')[-2]
  else:
    filename_pdf = save + path.split('/')[-2]
  filename_pdf = filename_pdf + '.pdf'
  c = canvas.Canvas(filename_pdf, pagesize=maxsize )
  l = len(filelist)
  for i in range(l):
    (w, h) =maxsize
    width, height = letter
    if fit == True:
      c.drawImage(filelist[i] , 0,0)
    else:
      c.drawImage(filelist[i] , 0,0,maxw,maxh)
    c.showPage()
  c.save()
def main():
  topdf(u'F:/gitplace/jpg2pdf/test',pictureType=['png','jpg'],save='F:/gitplace/jpg2pdf/test/新建文件夾')
if __name__ == '__main__':
  main()

GitHub地址:https://github.com/mrbeann/jpg2pdf

更多Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python文件與目錄操作技巧匯總》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程

希望本文所述對大家Python程序設(shè)計有所幫助。

相關(guān)文章

  • Python?OpenCV實現(xiàn)圖像形狀檢測

    Python?OpenCV實現(xiàn)圖像形狀檢測

    圖像形狀檢測是計算機(jī)視覺領(lǐng)域中的一項關(guān)鍵技術(shù),廣泛應(yīng)用于工業(yè)自動化、機(jī)器人視覺、醫(yī)學(xué)圖像處理等多個領(lǐng)域,本文將介紹如何使用Python?OpenCV實現(xiàn)圖像形狀檢測,需要的可以參考下
    2024-11-11
  • Python matplotlib實時畫圖案例

    Python matplotlib實時畫圖案例

    這篇文章主要介紹了Python matplotlib實時畫圖案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • Python中的異常處理詳解

    Python中的異常處理詳解

    這篇文章主要介紹了Python中的異常處理詳解,在編寫Python程序時,經(jīng)常會遇到各種運(yùn)行時錯誤,這些錯誤會導(dǎo)致程序終止并拋出異常。然而,有時我們希望程序能優(yōu)雅地處理這些錯誤,而不是直接崩潰,這就需要用到異常處理了,需要的朋友可以參考下
    2023-07-07
  • 利用Python解決Excel問題的最佳方案總結(jié)

    利用Python解決Excel問題的最佳方案總結(jié)

    python處理excel文件有很多方法,最開始接觸的是xlrd、xlsxwriter模塊,分別用于excel文件的讀、寫,后來又學(xué)習(xí)了openpyxl模塊,可以同時完成excel文件的讀、寫,下面這篇文章主要給大家介紹了關(guān)于利用Python解決Excel問題的最佳方案,需要的朋友可以參考下
    2023-04-04
  • python對列進(jìn)行平移變換的方法(shift)

    python對列進(jìn)行平移變換的方法(shift)

    今天小編就為大家分享一篇python對列進(jìn)行平移變換的方法(shift),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • 如何用 Python 制作一個迷宮游戲

    如何用 Python 制作一個迷宮游戲

    這篇文章主要介紹了如何用 Python 制作一個迷宮游戲,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2021-02-02
  • Python基于execjs運(yùn)行js過程解析

    Python基于execjs運(yùn)行js過程解析

    這篇文章主要介紹了Python基于execjs運(yùn)行js過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-11-11
  • Python驗證碼識別的方法

    Python驗證碼識別的方法

    這篇文章主要介紹了Python驗證碼識別的方法,涉及Python針對驗證碼圖片的相關(guān)分析與操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • Python web開發(fā)之用Tornado框架制作簡易表白墻網(wǎng)站

    Python web開發(fā)之用Tornado框架制作簡易表白墻網(wǎng)站

    這篇文章將用Python做Web開發(fā)。在Python當(dāng)中,WEB開發(fā)框架主要有三個,本文將利用Tornado框架做一個簡單的表白墻網(wǎng)站,感興趣的可以了解一下
    2022-02-02
  • Python轉(zhuǎn)換時間的圖文方法

    Python轉(zhuǎn)換時間的圖文方法

    在本篇文章里小編給大家整理的是關(guān)于Python轉(zhuǎn)換時間的方法以及具體步驟流程,需要的朋友們參考下。
    2019-07-07

最新評論

刚察县| 大洼县| 肃宁县| 铜梁县| 岗巴县| 廊坊市| 五指山市| 阜南县| 从化市| 池州市| 揭东县| 荥经县| 通山县| 江达县| 都昌县| 佛教| 龙江县| 桂林市| 绿春县| 贵州省| 巨鹿县| 丽江市| 乌兰察布市| 上林县| 会东县| 西畴县| 石嘴山市| 长垣县| 锡林郭勒盟| 五家渠市| 定结县| 泽普县| 涡阳县| 江阴市| 二手房| 嘉善县| 黎城县| 巴中市| 论坛| 呈贡县| 竹山县|