python3图片裁剪+转换pdf+压缩

2019-07-02 09:38:52 浏览数 (1)

本地大量长图,要发送给别人,所以要对图片进行裁剪 转换pdf 压缩

代码语言:javascript复制
  1 import zipfile
  2 import os
  3 from concurrent.futures import ThreadPoolExecutor
  4 from reportlab.lib.pagesizes import A4, landscape
  5 from reportlab.pdfgen import canvas
  6 from PIL import Image
  7 import time
  8 
  9 # 设置全局变量
 10 filepath = "../jpg/cartoon/"
 11 outzipfilepath = "../jpg/cartoonzip/"
 12 outpdffilepath = "../jpg/cartoonpdf/"
 13 cropimgpath = "../jpg/image/"
 14 
 15 
 16 # 找出文件夹下所有.xml后缀的文件
 17 def listfiles(rootdir, prefix='.xml'):
 18     file = []
 19     for parent, dirnames, filenames in os.walk(rootdir):
 20         if parent == rootdir:
 21             for filename in filenames:
 22                 if filename.endswith(prefix):
 23                     file.append(filename)
 24             return file
 25         else:
 26             pass
 27 
 28 
 29 # 创建文件夹
 30 def createjia(path):
 31     try:
 32         os.makedirs(path)
 33     except:
 34         pass
 35 
 36 
 37 # 这里是裁剪图片
 38 # 传入的是图片的名字
 39 def cropimg_tranpdf(imgname):
 40     # 新生成的文件夹的名字
 41     tempfilename = str(imgname.replace(".png", ""))
 42 
 43     # 创建保存裁剪后的图片的文件夹
 44     createpath = "../jpg/image/"   str(tempfilename)
 45     createjia(createpath)
 46 
 47     # 打开图片
 48     imgpath = filepath   imgname
 49     img = Image.open(imgpath)
 50 
 51     # 获得图片的宽高
 52     width = int(img.size[0])
 53     height = int(img.size[1])
 54 
 55     # A4的宽1240,高1754
 56     # 制作裁剪坐标
 57     countheight = (height // 1240)
 58 
 59     # 设置初始高度为1240
 60     newheight = 1240
 61 
 62     # 将要保存的pdf的位置和名字
 63     pdfname = str(outpdffilepath)   str(tempfilename)   ".pdf"
 64     # A4的大小
 65     # 发现A4会把长图压缩成1张A4的大小
 66     (w, h) = landscape(A4)
 67     # 保存pdf
 68     c = canvas.Canvas(str(pdfname), pagesize=landscape(A4))
 69     # number of page
 70     for i in range(0, countheight):
 71         newheight = newheight   1240
 72         # 裁剪的位置
 73         # (起始宽的位置,起始高的位置,裁剪宽度,裁剪高度)
 74         # 左上角的坐标为(0,0)
 75         region = (0, newheight - 1240, width, newheight)
 76         # 裁剪
 77         cropImg = img.crop(region)
 78         # 保存
 79         jpgname = str(i)   ".jpg"
 80         # 保存裁剪后的图片
 81         cropImg.save(str(createpath)   "/"   str(jpgname))
 82 
 83         # 写入的jpg将其组合成pdf
 84         filepath_jpgname = str(createpath)   "/"   str(jpgname)
 85         c.drawImage(filepath_jpgname, 0, 0, w, h)
 86         c.showPage()
 87     c.save()
 88     print("完成PDF:"   str(tempfilename))
 89 
 90 
 91 # 这里是裁剪和转化pdf的多进程
 92 # 开启多进程
 93 def threadingcrop_pdf(number):
 94     # 进程数
 95     pool = ThreadPoolExecutor(int(number))
 96     # 读取文件夹名字
 97     namelist = listfiles(filepath, "png")
 98 
 99     # 进程开跑
100     for name in namelist:
101         print(name)
102         pool.submit(cropimg_tranpdf, name)
103         # 太快电脑受不了
104         time.sleep(1)
105 
106 
107 
108 # 这里传入的是pdf的名字
109 # 写入压缩文件
110 def zipfiles(names):
111     # 需要压缩到的文件目录和名字
112     zipname = str(outzipfilepath)   str(names.replace(".pdf", ""))   ".zip"
113     # 需要压缩的文件位置和名字
114     name = outpdffilepath   names
115     files = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED)
116 
117     # 写入压缩包
118     files.write(name)
119     files.close()
120     print("完成压缩:"   str(zipname))
121 
122 
123 # 这里是压缩zip的多进程
124 # 开启多进程
125 def threadingzip(number):
126     # 先转化为pdf
127     threadingcrop_pdf(number)
128     # 进程数
129     pool = ThreadPoolExecutor(int(number))
130     # 读取文件名字
131     namelist = listfiles(outpdffilepath, "pdf")
132     # 进程开跑
133     for name in namelist:
134         print(name)
135         pool.submit(zipfiles, name)
136         # 太快电脑受不了
137         time.sleep(1)
138 
139 
140 if __name__ == '__main__':
141     # 多进程
142     #number = 1
143     #threadingzip(number)
144 
145     # 写入pdf
146     namelistpdf = listfiles(filepath, "png")
147     for name in namelistpdf:
148         print(name)
149         cropimg_tranpdf(name)
150 
151     # 写入zip
152     namelistzip = listfiles(outpdffilepath, "pdf")
153     for name in namelistzip:
154         print(name)
155         zipfiles(name)

0 人点赞